Skip to content

fix(rsc): send repeated server-component styles once, and explain edge-runtime failures - #5825

Merged
quantizor merged 16 commits into
6.xfrom
fix/rsc-shared-style-payload
Oct 12, 2026
Merged

quantizor merged 16 commits into
6.xfrom
fix/rsc-shared-style-payload

Conversation

@quantizor

Copy link
Copy Markdown
Contributor

Follow-up to #5808. Server components that repeat a styled component (a list, a carousel) render one <style> per instance. That is intentional, and it is what keeps styles alive when React reveals content from behind a Suspense boundary. This PR makes those repeats much cheaper in the data React sends for server components, and replaces two obscure edge-runtime crashes with clear errors.

What changes

Repeated styles are sent once in the server-component payload. Instances that resolve to the same CSS now reuse one <style> element object for the request. React 19's payload writer sends that element once and refers back to it for every repeat. The HTML is byte-identical, and every instance still renders its own tag, so a Suspense reveal never strands an element.

  • Styled components share by component ID plus generated class names. A repeat skips rebuilding its CSS.
  • createGlobalStyle shares by its ID plus the exact CSS, and returns the element inside an array. React only refers back to a whole repeated element when it sits inside an array or fragment.
  • An extension with no CSS of its own (for example styled(Base).attrs(...) with an empty template) produces exactly its base's class names. The component ID in the key keeps the two from sharing, since their CSS differs.

Edge runtime errors. Server rendering on the edge runtime is not supported: Next.js applies the package's browser file map to edge bundles, so the server loads the browser build. That used to end in createContext is not a function at next build, or document is not defined on the first request. Both now throw errors 19 and 20, which name the fix: remove export const runtime = 'edge', or set it to 'nodejs'.

Docs. The RSC style-injection spec now records why precedence is not used, how payload sharing works, and why the package has no exports map. The FAQ explains the repeated tags and the Node.js runtime requirement.

Measurements

Before After
Payload, 50 identical items (raw / gzip) 14,266 / 770 B 6,976 / 560 B
Payload, one global style x3 (raw / gzip) 579 / 189 B 309 / 187 B
Consumer production bundle, browser build (raw / gzip) +78 / +25 B (the two error guards; message text stays in the dev error map)
bench:rsc render time medians within run-to-run noise on every case

Verification

  • pnpm --filter styled-components test, build, and test:build pass.
  • New tests cover shared elements (same object for identical CSS, distinct for different CSS, components, StyleSheetManager namespaces and plugins, and base versus empty extension in both render orders), the Suspense fallback-plus-resolved shape, and both edge errors.
  • A Next.js 16 app built from the packed tarball: an edge server component fails next build with error 19, an edge client component fails the request with error 20, and Node-runtime routes render normally (Turbopack and webpack).
  • Sandbox in headless Chromium and WebKit: every styled element on the Suspense, global-style and RSC pages has its rule after reveal and after client navigation, no console errors or hydration warnings, and the server HTML styles everything with JavaScript off. The Suspense page's payload carries each of its 5 distinct styles once. Firefox was not covered by automation.

Related: #5824 tracks a separate escaping issue on Next.js 13.5 and 14.

…t-package bar

Corrects the Float claims in rsc-style-injection.md (plain styles keep
their attributes everywhere; precedence fails by bucket order, permanence,
attribute loss and position), specifies one shared style element per key
for the server-component payload, records why there is no exports map and
why edge server rendering is unsupported, and fixes the FAQ's advice for
long server-rendered lists.
An extension with no CSS of its own returns exactly its base's class
string, but its CSS wraps the base in :where() and its React key
differs, so whichever rendered first handed the other the wrong
element.
The hook body now asks buildRSCStyleCSS for the combined CSS and creates
the shared element only when it is non-empty. The comments describe the
element sharing instead of reading as no dedup.
…he guard comments

Errors 19 and 20 now also cover a server bundle that resolves the
browser build. The edge tests assert against the generated error map.
The redundant-emit warning names its count without 'Over', since it
fires at exactly the threshold.
The payload-dedup tests call the component's render function directly,
fold the namespace and stylisPlugins cases into one table, fold the
fixed-key checks into the distinct-element tests, and drop the
duplicated tag count and the any casts. The empty-leaf interpolation
case keeps its single-line template so the leaf earns no class of its own.
@changeset-bot

changeset-bot Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 12045e8

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 new issues found.

Reviewed changes

Reviewed the complete PR at 3056562 (all 16 changed files): the RSC payload-sharing logic, its keys and React-element lifecycle, the two new edge-runtime guards, the error catalog, tests, and docs.

  • Styled-component payload dedup: a request-scoped React.cache Map keyed on component ID + generated class string now reuses one <style> element object across instances with identical CSS, while every instance still emits its own tag. buildRSCStyleCSS is the extracted builder.
  • Global-style payload dedup: same pattern keyed on global ID + exact CSS; the returned element is array-wrapped and its React key is now constant so the object can be shared.
  • Edge-runtime errors 19/20: a module-load guard in constants.ts (!IS_RSC && !React.createContext) and a typeof document guard in makeStyleTag, plus catalog entries and regenerated errors.ts (verified in sync).
  • Tests: shared-element identity cases (identical vs distinct CSS, distinct components, StyleSheetManager namespaces/plugins, base-vs-empty extension in both orders, Suspense fallback+resolved DOM), a node-env makeStyleTag test, and a browser-build edge-guard tree-shake test.
  • Docs: rsc-style-injection.md "Payload dedup" + precedence rationale, build-architecture.md "Edge runtime" + no-exports-map rationale, FAQ, and two changesets.

I verified the load-bearing details against the source: the style key is sound because generatedClassName embeds both the component ID (via baseHash) and the stylis configuration hash (namespace, plugins, vendor prefixing) from utils/stylis.ts, so an identical key implies identical emitted CSS; makeStyleTag is only reachable from the DOM tag classes (Node SSR uses VirtualTag), so error 20 fires only where intended; and the new constants → error import introduces no runtime cycle because error.ts's types import is type-only and elided.

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

@quantizor
quantizor merged commit 209317c into 6.x Oct 12, 2026
8 checks passed
@quantizor
quantizor deleted the fix/rsc-shared-style-payload branch October 12, 2026 16:31
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.

1 participant