Repository navigation
fix(rsc): keep StyleSheetManager settings per request in server components - #5842
Conversation
… ordering contract
StyleSheetManager kept its RSC override and memoized stylis instance in module state, so a manager's plugins, namespace, or vendor prefixes could reach a later request. The override now lives in a React.cache slot per request, and the stylis memo is keyed on the plugins array, namespace, and enableVendorPrefixes. A namespace-only manager clears the memo so the next plugin manager rebuilds it.
…cache lookup per render
🦋 Changeset detectedLatest commit: 663e678 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
- Per-request
StyleSheetManagerslot (packages/styled-components/src/models/StyleSheetManager.tsx) — replaced the shared module-levelrscContextOverride+ separateensureSheetResetwith a request-scoped{ override }slot built through the existingcreateRSCCache, whose factory creation is now the per-request sheet reset. Concurrent requests no longer clear each other's settings. - Stylis memo invalidation — the module-level
rscCachedStyliskey now includesnamespaceandenableVendorPrefixes, and the namespace/prefix-without-plugins branch resets all three cache keys, so a manager cannot inherit a prior manager's options off a reused plugins array. - Tests (
styled.rsc.test.tsx) — added a store-swap harness to model concurrent requests plus four cases (cross-request slot leak, namespace drop, vendor-prefix drop, plugin restore across a namespace-only manager). - Docs + changeset —
docs/rsc-style-injection.mddocuments the request-scoped slot, the React-18 no-React.cachefallback, and the ordering contract;.changeset/rsc-manager-per-request.mddescribes the two user-visible fixes.
I verified the tests are real regression coverage: with only StyleSheetManager.tsx reverted to the base revision, exactly the four new tests fail (77 others pass). The full main suite passes (983/60 suites), and tsc --noEmit -p packages/styled-components/tsconfig.json is clean. The single-request behavior and the mainSheet shared-reset invariant are unchanged; the documented limitations (#5838 layout managers, no reset without React.cache) are accurately scoped as pre-existing.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

This affects apps that use
StyleSheetManagerin React Server Components, for example Next.js App Router pages, on 6.x.What changes
Settings no longer drop out under load. When several requests render at the same time on one server, a server component that awaits data could lose its
StyleSheetManagersettings partway through rendering. WithstylisPluginRSC, that meant selectors like:first-childwere sometimes sent unrewritten, so they matched the wrong element depending on server traffic. Each request now keeps its own settings.A reused plugins array no longer picks up another manager's options. If you pass the same
stylisPluginsarray (for example a module-level constant) to more than one manager, a manager withoutnamespaceorenableVendorPrefixescould inherit those options from a manager that rendered before it. Each manager now gets exactly the options it was given.What does not change
This does not fix the client-navigation case in #5838. In server components, a manager affects what renders after it in the same request. Next.js does not re-render shared layouts or templates when you navigate between pages, so a manager placed in a layout does not reach the new page. Until that is addressed, render the manager in each page (or in a component every page renders), before any
await.Output for a single request is byte-for-byte unchanged.
Verification
Refs #5838