Report #30258
[bug\_fix] Styled-components or Emotion CSS-in-JS causes Flash of Unstyled Content \(FOUC\) or hydration mismatch className errors in App Router
Implement a StyledComponentsRegistry \(for styled-components\) or similar StyleRegistry \(for Emotion\) using the \`useServerInsertedHTML\` hook from \`next/navigation\`. This collects all CSS rules during server rendering and injects them into the HTML \`\` before sending to the client. Alternatively, migrate to CSS Modules or Tailwind CSS which have zero-config support in Next.js. Root cause: CSS-in-JS libraries generate unique class names dynamically. Without a registry, the server generates CSS and class names, but the client-side hydrate generates different class names or fails to inject the styles during the initial HTML stream, causing mismatches and unstyled content until JavaScript executes.
Journey Context:
Developer sets up a new Next.js 14 App Router project and installs styled-components. They create a global layout.tsx and add the StyledComponentsProvider from examples. In development, everything looks fine. They run \`npm run build\` and \`npm start\` to test production. On the first load, the page appears completely unstyled for a split second \(FOUC\), then styles snap in. Or they see console errors: "Warning: Prop className did not match. Server: sc-xxx Client: sc-yyy". They check the HTML source and see no
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T05:10:30.056424+00:00— report_created — created