Repository navigation
fix(rsc): keep >, quotes and & as written in server component styles on React 18 - #5881
Conversation
…on React 18 React before 19 HTML-escapes <style> text children during server rendering, so Next.js 13.5 and 14 sent '.a > .b' to the browser. Pass the CSS through dangerouslySetInnerHTML on those versions, with </style escaped as \3C/style; React 19 keeps text children. Fixes styled-components#5824. Co-Authored-By: Rahul Kashayp <[email protected]>
🦋 Changeset detectedLatest commit: 6b6cbc1 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
ℹ️ No blocking issues — one minor test-robustness suggestion inline.
Approach: sound — the helper branches on React.version so only the pre-19 path switches to raw HTML, leaving React 19's own <style> text handling and its closing-tag guard in place. I verified the premise against both renderers: react-dom 18.3.1 emits .a>.b for style children and writes dangerouslySetInnerHTML raw, while react-dom 19.2.3 writes raw children and rewrites </style to </\73 tyle, so the version split is the right seam.
Reviewed changes
utils/styleTagContent.ts— new helper: React < 19 getsdangerouslySetInnerHTMLwith</stylerewritten as the CSS escape\3C/style; React 19+ keepschildren.models/StyledComponent.tsandconstructors/createGlobalStyle.ts— the only two inline-<style>emission sites now spread the helper's props. The other<style>producer,ServerStyleSheet.getStyleElement, already useddangerouslySetInnerHTML, so no site is missed.- RSC tests — new child-combinator/quote/ampersand and tag-close cases, previously escaped snapshots updated to raw CSS, plus a unit test covering both version branches.
- Changeset — patch entry scoped to Next.js 13.5/14 and stating that a value still cannot close the tag.
I ran the web suite against react 18.3.1 from the 6.x lockfile (1030 tests pass, Prettier and tsc --noEmit clean) and confirmed the new cases go red when the helper is forced back to the children path, so they do catch the regression.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
…g-tag escape, and avoid spread allocation
|
Thanks @itisrmk for tracking this down and putting together such a clean fix! I pushed a small follow-up commit ( |
…utes data-* augmentation
There was a problem hiding this comment.
ℹ️ No critical issues — one minor test-robustness suggestion inline.
Approach: sound, and the refinement in this delta is the right one — escaping only the s/S of </style matches React 19's own emitter, so the escaped text decodes back to the original CSS and the close-tag expectation now holds on both the pre-19 raw path and React 19's children path.
Reviewed changes
- Gated the version check on
IS_RSCand folded<style>creation intocreateStyleElement, so the standalone, browser, and native bundles drop the branch (confirmed againstrollup.config.mjs, which replaces thetypeof React.createContext === 'undefined'expression in all three). - Replaced the
\3C/stylerewrite with</\73 tyle/</\53 tyle, preserving letter case and preceding backslashes; this matchesreact-dom19.2.3'sstyleReplacerexactly. - Added
styleTextaccessors to both RSC suites, removing the unconditionalprops.dangerouslySetInnerHTML.__htmlreads that kept them React-18-only. - Added unit coverage for the non-RSC branch, an experimental version string, and a missing
React.version. - Added a browser-build tree-shaking assertion for the version check and the escape.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Reviewed the two commits since the prior review (68646de0, 6b6cbc14); the earlier concern about the treeshake assertion is now resolved in the code.
- Narrowed
StyleElementPropstoReact.StyleHTMLAttributes<HTMLStyleElement> & Record<string, string>, so the props object satisfiesReact.Attributes'keywithout a type escape. Type-only, no runtime surface;tsc --noEmiton the package reports no error in the file or either call site. - Replaced the vacuous
not.toContain('</(s)(tyle)')assertion with(s)(tyle)and added aserverESMpositive control (dist/styled-components.esm.jsis the server build inrollup.config.mjs), so the dead-code test now fails when the escape survives in the browser bundles and fails when the server build loses it.
ℹ️ GitHub took this as a comment because it requires explicit repository access to approve or block a pull request here.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
ℹ️ No blocking issues — one minor test-coverage note inline.
Approach: sound. The incremental delta addresses the prior review's inline point: the tree-shaking test now asserts the server build as a positive control and uses the (s)(tyle) capture-group marker that survives minification, so the browser assertions can actually fail when the helper is not eliminated. The type narrowing also fixes a real error: with React.Attributes augmented by a data-* index signature, the old [key: string]: unknown signature is incompatible with createElement's Attributes & P parameter, while Record<string, string> is assignable.
Verified locally: the built server ESM/CJS bundles contain (s)(tyle) and parseInt(...version, while the browser ESM/CJS bundles contain neither; pnpm typecheck, test:types, test:types:dist, the build's test:build tree-shaking suite, and the RSC/helper suites all pass. A probe that augments React.Attributes with a data-* index signature fails on the old type and compiles on the new one.
Note: no repo fixture exercises that React.Attributes augmentation shape (the existing test-types/template-index-signature.tsx augments HTMLAttributes), so reverting the type change leaves every gate green. See the inline comment.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
| */ | ||
| const REACT_ESCAPES_STYLE_TEXT = IS_RSC && !(parseInt(React.version, 10) >= 19); | ||
|
|
||
| type StyleElementProps = React.StyleHTMLAttributes<HTMLStyleElement> & Record<string, string>; |
There was a problem hiding this comment.
This narrowing fixes a real consumer-augmentation error (the old [key: string]: unknown is incompatible with a data-* index signature on React.Attributes), but no repo fixture exercises that shape: test-types/template-index-signature.tsx augments HTMLAttributes, not Attributes, and reverting this line leaves typecheck, test:types, and test:types:dist green. Add a type fixture that augments React.Attributes with a data-* index signature so the change is locked; without it the next cleanup can silently restore the incompatible signature.

Fixes #5824
Server components on Next.js 13.5 and 14 render their styles as an inline
<style>tag, and the React 18.3 canary those versions bundle HTML-escapes the tag's text, so the server HTML contained.a > .b{content:"x"}and the browser dropped those rules. With this change, on React versions before 19 the CSS reaches the HTML exactly as written (.a > .b{content:"x"}), for both styled components andcreateGlobalStyle. A style value still cannot close the tag: a</styleinside one is written as the CSS escape\3C/style, which the browser reads back as the same character.On React 19 (Next.js 15 and later) the output and the server-component payload are unchanged.
This targets
6.x, sincemainrequires React 19 where the bug does not occur.Tests
<style>tag. They fail on6.x(the test environment runs React 18.3.1, which escapes the same way) and pass with the change.>,') now expect the CSS as written.typecheck,test:types,prettier:checkandpnpm build(includingtest:build) pass.Bundle size, gzip, before / after: browser ESM 14135 / 14143 B, server ESM 15781 / 15841 B, server CJS 15932 / 15993 B, UMD min unchanged.