Sergio Garcia Gallego
7 min read ·
What the renderer does with your editors' formatting
Sergio Garcia Gallego
7 min read ·
In July 2026, at MAJOR Digital, I rebuilt the component that renders rich text for a client: a large UK trade union whose site was moving to Next.js and Storyblok. We were switching from the community storyblok-rich-text-react-renderer package to the server-side renderer in Storyblok's own React SDK.
I had two requirements:
The version I shipped did both by cleaning the rich text before rendering it. At the time, I believed the renderer turned alignment and colour into an inline style string, which React rejects on the server. When I came back to it for this post, I tested that properly, and it turned out to be half right.
The @storyblok/react docs (opens in a new tab) (now on v8) say the StoryblokRichText component works in both Server and Client Components. v7, which we used in July, had a separate useStoryblokServerRichText hook under @storyblok/react/rsc, but both use the same renderer underneath.
To change how part of the rich text renders, you pass a components map. Each key is either a node (a block, like a paragraph or heading) or a mark (formatting on a bit of text, like bold or a link), and each value is your own React component. v8 also has a data prop, which passes your own data to all of those components as context.data.
What none of the pages I read explain is what the default renderer does with an editor's formatting, or with attributes it doesn't recognise. That goes for the React SDK docs, the @storyblok/richtext docs (opens in a new tab) and the v4 to v5 migration guide (opens in a new tab).
Storyblok's rich text editor is built on Tiptap, so alignment works like Tiptap's TextAlign extension (opens in a new tab): it adds a text align attribute to paragraphs and headings. Text colour and highlights are saved as textStyle and highlight marks.
To see what comes out the other end, I rendered each one through the default renderer with react-dom/server:
textAlign: 'center' renders as <p style="text-align:center">.textAlign: 'right' renders as <h2 style="text-align:right">.textStyle mark with color: '#c00' renders as <span style="color:#c00">.highlight mark with color: '#ff0' renders as <mark style="background-color:#ff0">.styled mark with class: 'brand' renders as <span class="brand">.None of these crash, because @storyblok/richtext turns them into a proper React style object. The catch is that an inline style (CSS written straight onto the element) beats your stylesheet. Say an editor sets some text to #000000 on a light background. Switch to dark mode, and that text is now black on black. Alignment is the same: a centred paragraph ignores whatever alignment its parent component expects.
There's a second catch if your site has a strict Content Security Policy (CSP), a header that tells the browser which styles and scripts it's allowed to use. MDN's style-src page (opens in a new tab) says inline style attributes are blocked too, unless you allow 'unsafe-inline', a nonce or a hash, and style-src-attr (opens in a new tab) covers inline styles on individual elements. I checked this in the demo for this post: with style-src 'self', the centred heading shows up left-aligned, and the browser logs a CSP violation for every styled element.
The one I'd keep is the styled mark. It renders a class name instead of a style, so your CSS decides what the brand class looks like in each theme.
So where does the crash come from? Here's how @storyblok/richtext handles an attribute it doesn't know about:
const attrName = attrMap[key] ?? key;
if (attrName === 'custom' && type === 'link' && typeof value === 'object' && value !== null) {
for (const [customKey, customValue] of Object.entries(value)) rest[customKey] = String(customValue);
return;
}
if (typeof value === 'object' && value !== null) {
rest[attrName] = JSON.stringify(value);
return;
}
rest[attrName] = value;
In other words, if the renderer doesn't recognise an attribute, it puts it straight onto the HTML element. That's fine for most attributes, but not for style. React's style prop (opens in a new tab) has to be an object of CSS styles, so a CSS string breaks it. Links have the same problem, because the keys in a link's custom object are copied onto the <a> as strings. So a link with custom: { style: 'font-weight: bold' } gives you this on the server:
Error: The `style` prop expects a mapping from style properties to values, not a string.
In a Server Component, that error fails the whole render. In the demo, the route returns a 500, so one badly formed link takes the entire page down.
I tested this on the versions that build used in July (@storyblok/react 7.2.0 with @storyblok/richtext 5.2.0), the versions it runs today (7.5.2 with 5.5.2), and v8.0.0. All three behave the same.
Storyblok's importer won't create this. htmlToStoryblokRichtext is the parser you'd use to import content from another CMS, and when I gave it HTML with inline styles, it dropped them and logged a warning:
[StoryblokRichText] - `style` "font-size:20px" on `<span>` can not be transformed to rich text.
So a raw style string has to come from something that writes the JSON itself, like a custom import script or the Management API. I tried the Management API on a real space: it saved a link with custom: { style: 'font-weight: bold' } exactly as I sent it, the Content Delivery API served it back, and the page returned a 500. Content rarely arrives that way, but when it does, nothing along the way stops it.
The good news is both problems have the same fix: decide which formatting your site allows, and strip out the rest before the JSON reaches the renderer.
Rich text is a tree. Each node can have attrs (settings like alignment), marks (formatting) and content (more nodes inside it), so one function that calls itself on each child can clean the whole thing:
const blockedAttrs = new Set(['textAlign', 'style', 'color']);
const blockedMarks = new Set(['textStyle', 'highlight']);
const cleanAttrs = (attrs: Attrs = {}) => {
const kept = Object.fromEntries(
Object.entries(attrs).filter(([key]) => !blockedAttrs.has(key))
);
if (kept.custom && typeof kept.custom === 'object') {
kept.custom = cleanAttrs(kept.custom as Attrs);
}
return kept;
};
export default function cleanRichText(node: RichText): RichText {
return {
...node,
...(node.attrs && { attrs: cleanAttrs(node.attrs) }),
...(node.marks && {
marks: node.marks
.filter(mark => !blockedMarks.has(mark.type))
.map(mark => ({ ...mark, attrs: cleanAttrs(mark.attrs) })),
}),
...(node.content && { content: node.content.map(cleanRichText) }),
};
}
Here, blockedAttrs and blockedMarks hold your design decisions, and anything listed in them gets removed. This version keeps bold, italic, links and styled classes, and drops alignment, free colour and highlights. In the client version, I also stripped styled, so keep it only if your design system gives editors a set of classes to pick from.
The function also returns new objects instead of changing the original. That matters in a Server Component, where the same fetched story can feed several components.
The second requirement was showing the same rich text at different sizes. On the client build (v7), the wrapper component took heading and paragraph props, and built its components map inside the render so each custom component could read them. There was also an inline prop that dropped the paragraph wrapper, for rich text sitting inside another element.
Now, in v8, the data prop is the documented way to do this. The wrapper passes the sizes in, and each custom component reads them from context.data:
const Heading = ({
attrs,
children,
context,
}: StoryblokReactRichTextProps<'heading', TypoData>) => {
const Tag = `h${attrs?.level ?? 2}` as 'h2';
return <Tag className={headingSizes[context.data.heading]}>{children}</Tag>;
};
<StoryblokRichText
document={clean ? cleanRichText(content) : content}
components={{ heading: Heading, paragraph: Paragraph }}
data={{ heading, paragraph }}
/>
Because TypoData types context.data, TypeScript flags it if the wrapper forgets to pass a heading size. In the demo, the same document renders twice on one page, once large and once small.
The demo (opens in a new tab) is a Next.js 16 app on @storyblok/react 8.0.0, with Playwright tests that run against a production build. Its rich text includes everything above: an aligned heading and paragraph, a colour, a highlight and a link with a custom style attribute. There are seven tests, and here are the first two:
test('rendering the raw document fails the whole page', async ({ request }) => {
const response = await request.get('/raw');
expect(response.status()).toBe(500);
});
test('the cleaned document renders without inline styles', async ({ page }) => {
const response = await page.goto('/clean');
expect(response?.status()).toBe(200);
await expect(page.locator('main [style]')).toHaveCount(0);
await expect(page.locator('main mark')).toHaveCount(0);
await expect(
page.getByRole('link', { name: 'full terms' }).first()
).toHaveAttribute('href', '/terms');
});
Three more check the inline styles from earlier, that a style-src 'self' policy blocks them, and that each instance gets its own heading and paragraph classes. The last two run against a real Storyblok space, using a story that pnpm seed writes through the Management API, and they're skipped if no token is set. On top of that, pnpm compare renders the same rich text through 7.2.0, 7.5.2 and 8.0.0 and prints either the HTML or the error. That's where the outputs and version comparison in this post come from.
Still on v7? cleanRichText works the same with useStoryblokServerRichText, since it only touches the JSON. For different sizes per instance, use the props approach from earlier: 7.2.0 has no data option, and in 7.5.2 the server hook doesn't pass it through to your components.