Repository navigation
fix(rsc): send repeated server-component styles once, and explain edge-runtime failures - #5825
Conversation
…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.
…with identical classes
…yload references it whole
…rect the styleSheet.server rationale
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 detectedLatest commit: 12045e8 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 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.cacheMap 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.buildRSCStyleCSSis 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 atypeof documentguard inmakeStyleTag, plus catalog entries and regeneratederrors.ts(verified in sync). - Tests: shared-element identity cases (identical vs distinct CSS, distinct components,
StyleSheetManagernamespaces/plugins, base-vs-empty extension in both orders, Suspense fallback+resolved DOM), a node-envmakeStyleTagtest, and a browser-build edge-guard tree-shake test. - Docs:
rsc-style-injection.md"Payload dedup" +precedencerationale,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.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

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.createGlobalStyleshares 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.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
browserfile map to edge bundles, so the server loads the browser build. That used to end increateContext is not a functionatnext build, ordocument is not definedon the first request. Both now throw errors 19 and 20, which name the fix: removeexport const runtime = 'edge', or set it to'nodejs'.Docs. The RSC style-injection spec now records why
precedenceis not used, how payload sharing works, and why the package has noexportsmap. The FAQ explains the repeated tags and the Node.js runtime requirement.Measurements
bench:rscrender timeVerification
pnpm --filter styled-components test,build, andtest:buildpass.StyleSheetManagernamespaces and plugins, and base versus empty extension in both render orders), the Suspense fallback-plus-resolved shape, and both edge errors.next buildwith error 19, an edge client component fails the request with error 20, and Node-runtime routes render normally (Turbopack and webpack).Related: #5824 tracks a separate escaping issue on Next.js 13.5 and 14.