Skip to content

fix(rsc): keep StyleSheetManager settings per request in server components - #5842

Merged
quantizor merged 5 commits into
6.xfrom
fix/rsc-manager-per-request
Nov 3, 2026
Merged

quantizor merged 5 commits into
6.xfrom
fix/rsc-manager-per-request

Conversation

@quantizor

Copy link
Copy Markdown
Contributor

This affects apps that use StyleSheetManager in 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 StyleSheetManager settings partway through rendering. With stylisPluginRSC, that meant selectors like :first-child were 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 stylisPlugins array (for example a module-level constant) to more than one manager, a manager without namespace or enableVendorPrefixes could 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

  • New tests cover two overlapping requests and each way a reused plugins array could carry over options. They fail on the current release and pass with this change. All existing snapshots are unchanged.
  • In a Next.js 16 app, a page with a manager and a slow child, rendered while a second request arrived: 0 of 5 renders came out correct before this change, and 5 of 5 after.
  • Server bundle size grows by 5 to 14 bytes gzipped. Browser bundles are unchanged. Server render benchmarks show no change beyond run-to-run noise.

Refs #5838

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.
@changeset-bot

changeset-bot Bot commented Oct 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 663e678

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

  • Per-request StyleSheetManager slot (packages/styled-components/src/models/StyleSheetManager.tsx) — replaced the shared module-level rscContextOverride + separate ensureSheetReset with a request-scoped { override } slot built through the existing createRSCCache, 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 rscCachedStylis key now includes namespace and enableVendorPrefixes, 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.md documents the request-scoped slot, the React-18 no-React.cache fallback, and the ordering contract; .changeset/rsc-manager-per-request.md describes 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.

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

@quantizor
quantizor merged commit ed7b199 into 6.x Nov 3, 2026
8 checks passed
@quantizor
quantizor deleted the fix/rsc-manager-per-request branch November 3, 2026 03:22
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