AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Experimental.InlineCss Duplicates Global Stylesheet 3X Due To Empty InjectedCSS Set In GetGlobalErrorStyles

With experimental.inlineCss enabled, the full global stylesheet is embedded three times in every prerendered page: once in the SSR <style> tag, once in the RSC payload segment seed data (documented limitation), and a third time under the GlobalError entry because getGlobalErrorStyles passes a fresh empty Set as injectedCSS to createComponentStylesAndScripts, causing re-inlining of all root layout styles. This significantly inflates HTML size (e.g., ~1.3MB extra per page for a 435KB Tailwind sheet), increasing parse/memory cost and ISR write units.

highConfidence 85%Next.jsAffected V16.2.10Affected V16.3.3Affected V16.4.0-Canary.12

Origin Analysis

In packages/next/src/server/app-render/app-render.tsx, getGlobalErrorStyles invokes createComponentStylesAndScripts with injectedCSS: new Set(), ignoring the Set of CSS already inlined during the root layout render. This bypasses deduplication and causes every global stylesheet to be injected again under the GlobalError flight payload, even when no custom global-error.tsx exists.
1. Clone https://github.com/winkelmeyer/next-inlinecss-global-error-duplication 2. Run npm install 3. Run npx next build 4. Run grep -c -- '--repro-marker-inlinecss-dup' .next/server/app/index.html Observe the marker appears 3 times, indicating three full copies of the global stylesheet.

Fixing Code Block

Edge Case Audit

The fix assumes rootLayoutInjectedCSS is the exact Set used for root layout in this request context and is not cleared or replaced before getGlobalErrorStyles runs. If the Set is shared across concurrent requests or mutated asynchronously during streaming, it could lead to race conditions or missing styles. Ensure the Set is per-request. The change only eliminates the third copy; the documented second copy in the RSC payload remains. Rollback by reverting to new Set() if unexpected missing CSS occurs in global-error boundaries.

Ecosystem Topology