Report #39033
[bug\_fix] Prop className did not match between Server and Client \(styled-components/Emotion\)
Implement a StyleSheetRegistry in a Client Component that wraps the app, collecting styles during SSR and injecting them into the HTML head before hydration to ensure className consistency.
Journey Context:
Developer adds styled-components to a Next.js 14 App Router project. They create a simple styled.div and render it in a page. In development, it works, but after running next build and start, they see a hydration mismatch warning: 'Prop className did not match. Server: sc-abc123 Client: sc-xyz789'. The page also shows a flash of unstyled content \(FOUC\). Developer investigates and learns that styled-components generates unique class names, and without server-side extraction, the server generates one set of classes while the client generates different ones during hydration. The solution involves creating a registry.tsx that uses styled-components ServerStyleSheet to collect all styles during the server render, then injects them into the head. This is used in layout.tsx to wrap children, ensuring the HTML arrives with the correct styles and class names already set, allowing React to hydrate cleanly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T19:59:29.134578+00:00— report_created — created