Skip to content

feat(rsc): add configureServerStyles to set plugins for every server component - #5865

Merged
quantizor merged 2 commits into
6.xfrom
feat/6x-configure-server-styles
Nov 6, 2026
Merged

quantizor merged 2 commits into
6.xfrom
feat/6x-configure-server-styles

Conversation

@quantizor

@quantizor quantizor commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Plugins set with a StyleSheetManager in a shared Next.js layout miss pages reached by client-side navigation, because Next doesn't re-render that layout on the way to the new page. A page can look right on a direct load and lose its plugin output after navigating to it (#5838). This adds a way to set plugins that doesn't depend on what rendered first.

What changes

configureServerStyles sets the plugins every server component is styled with, for the whole server process:

// app/layout.tsx
import { configureServerStyles, stylisPluginRSC } from 'styled-components';

configureServerStyles({ stylisPlugins: [stylisPluginRSC] });

It reaches every server-rendered page and nested layout, including prerendered pages and pages reached by client-side navigation.

A StyleSheetManager in a server component starts from these plugins. Its stylisPlugins prop replaces them, and stylisPlugins={[]} drops them. A manager with no configured plugins behaves as it did.

It applies to server components only. Client components take their plugins from a StyleSheetManager rendered on the client, and calling configureServerStyles in the browser does nothing apart from a development warning.

Also fixed

stylisPluginRSC lost its name in production bundles. Together with the unreleased plugin name check, that failed production Next.js builds that use it. This should land before the next 6.x release.

Cost

Bundle size, gzip -9 bytes:

Bundle Before After Change
browser ESM 14,004 14,153 +149
browser CJS 14,161 14,312 +151
server ESM 15,591 15,791 +200
server CJS 15,733 15,950 +217
standalone min 12,906 12,914 +8

The browser bundles grow by the function's warning and export; none of the server code reaches them.

Server component render time with nothing configured, from the repo's RSC benchmark: median of 6 rounds on the same machine, run from source, with the build-time server flag read once at load to approximate the published bundle, where it is a constant:

Workload Before After Ratio
20 cards + badges + keyframes + global style 123.46 µs 125.00 µs 1.01x
50 identical static components 53.19 µs 54.64 µs 1.03x
50 dynamic components 64.10 µs 66.67 µs 1.04x
Plain React controls 1.00x

That is about 30 to 50 ns per styled component, near the limit of what this benchmark resolves (repeat runs of one tree vary by up to 4%). It pays for reading the configuration on each render, so that a call from another copy of the library in the same process takes effect.

Testing

  • Web, native, and build suites pass, with new tests for each manager combination, a separate library copy, changed configuration, and an unnamed plugin
  • Checked in a real Next.js app in development and as a production build: a direct load and a client-side navigation back to the page produce the same rewritten CSS and the same class name. Before this change, the navigation produced unrewritten CSS and a different class name

@changeset-bot

changeset-bot Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2e2bce2

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 Minor

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.

ℹ️ Minor suggestions only.

Reviewed changes

  • configureServerStyles — new process-wide plugin configuration for server components, held on globalThis under a major-versioned Symbol.for key, read through serverDefaults(), with a development-only browser warning.
  • Manager inheritance — a server StyleSheetManager starts from the configured plugins, replaces them with its own stylisPlugins, and stylisPlugins={[]} drops them, including the override-clear comparison moving from mainStylis to the server default.
  • stylisPluginRSC name — Object.defineProperty becomes an annotated initializer, so the pure annotation only drops it together with the plugin.
  • Tests — RSC matrix (manager combinations, [] scoped per request, separate library copy, reconfiguration, unnamed plugin on every request, StyleSheetConsumer), the one-time browser warning, and a production webpack bundle asserting the plugin name.
  • Docs / changesets / sandbox — README section, docs/rsc-style-injection.md subsection, plugin JSDoc, two changesets, and the sandbox moved from a /rsc layout manager to a root-layout call.

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

Comment thread docs/rsc-style-injection.md Resolved

@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

  • docs/rsc-style-injection.md placement — the ### configureServerStyles subsection now sits below the manager section's trailing nonce paragraph, so nonce keeps reading as part of the StyleSheetManager override rather than the new API.
  • Docs list formatting — that subsection's bullets are hard-wrapped and period-terminated, matching the file's convention, and the slot paragraph points page-wide plugins at the new section.

The one prior minor finding is addressed, and the commit since the last review changes documentation only.

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

@quantizor
quantizor force-pushed the feat/6x-configure-server-styles branch from c43adbf to 2e2bce2 Compare November 6, 2026 16:43

@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

Re-review of the two commits after the branch was rebased onto 6.x, which brought the per-request StyleSheetManager slot into the base.

  • Configured defaults feed the slot — serverDefaults() now backs the slot-empty read paths, so a server component whose layout did not render in that request still sees the plugins set by configureServerStyles.
  • Override-clear comparison — the manager compares its resolved stylis instance against the server default rather than the plugin-free one, so stylisPlugins={[]} keeps writing an override under a configured default. The existing [], replace, and replace-then-reconfigure tests still pass unchanged.
  • Docs — the stylisPlugins={[]} paragraph now matches the base's behavior (falling back to a plugin-free instance that keeps namespace / enableVendorPrefixes), and the configureServerStyles section is unchanged.

I ran the RSC and manager suites after the rebase (122 tests, green) and probed the combinations the base change made newly reachable: [] plus a namespace under a configured default, a nested manager inheriting a parent [] override, a manager repeating the configured array identity, and a configuration call made after a component was defined. All behaved as the docs describe.

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

@quantizor
quantizor merged commit f17f6c0 into 6.x Nov 6, 2026
8 checks passed
@quantizor
quantizor deleted the feat/6x-configure-server-styles branch November 6, 2026 16:49
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