Skip to content

fix(rsc): keep >, quotes and & as written in server component styles on React 18 - #5881

Merged
quantizor merged 4 commits into
styled-components:6.xfrom
itisrmk:fix/5824-rsc-style-escaping
Nov 24, 2026
Merged

quantizor merged 4 commits into
styled-components:6.xfrom
itisrmk:fix/5824-rsc-style-escaping

Conversation

@itisrmk

@itisrmk itisrmk commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

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 &gt; .b{content:&quot;x&quot;} 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 and createGlobalStyle. A style value still cannot close the tag: a </style inside 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, since main requires React 19 where the bug does not occur.

Tests

  • New cases for a child combinator, a quoted value and an ampersand in a styled component and in a global style, plus a value that tries to close the <style> tag. They fail on 6.x (the test environment runs React 18.3.1, which escapes the same way) and pass with the change.
  • Existing RSC assertions that had locked in the escaped output (&gt;, &#x27;) now expect the CSS as written.
  • A unit test checks both branches, React 18 (including the Next.js 14.2 canary version string) and React 19.
  • Full web suite (1030 tests), native suite, typecheck, test:types, prettier:check and pnpm build (including test: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.

…on React 18

React before 19 HTML-escapes <style> text children during server rendering, so Next.js 13.5 and 14 sent '.a &gt; .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-bot

changeset-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6b6cbc1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
styled-components Patch

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

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ 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&gt;.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 gets dangerouslySetInnerHTML with </style rewritten as the CSS escape \3C/style; React 19+ keeps children.
  • models/StyledComponent.ts and constructors/createGlobalStyle.ts — the only two inline-<style> emission sites now spread the helper's props. The other <style> producer, ServerStyleSheet.getStyleElement, already used dangerouslySetInnerHTML, 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.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@quantizor

Copy link
Copy Markdown
Contributor

Thanks @itisrmk for tracking this down and putting together such a clean fix! I pushed a small follow-up commit (ac20e530) to gate the React version check on IS_RSC (so browser builds drop it completely), escape the s/S in </style to preserve letter case and preceding backslashes, avoid the extra spread allocation, and cover createGlobalStyle closing tags and React 19 test portability. Will merge once CI and review checks settle.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ 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_RSC and folded <style> creation into createStyleElement, so the standalone, browser, and native bundles drop the branch (confirmed against rollup.config.mjs, which replaces the typeof React.createContext === 'undefined' expression in all three).
  • Replaced the \3C/style rewrite with </\73 tyle / </\53 tyle, preserving letter case and preceding backslashes; this matches react-dom 19.2.3's styleReplacer exactly.
  • Added styleText accessors to both RSC suites, removing the unconditional props.dangerouslySetInnerHTML.__html reads 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.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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 StyleElementProps to React.StyleHTMLAttributes<HTMLStyleElement> & Record<string, string>, so the props object satisfies React.Attributes' key without a type escape. Type-only, no runtime surface; tsc --noEmit on 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 a serverESM positive control (dist/styled-components.esm.js is the server build in rollup.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.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@quantizor
quantizor merged commit 7831e15 into styled-components:6.x Nov 24, 2026
8 checks passed

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ 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.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using 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>;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants