Report #45105
[bug\_fix] Prop className did not match \(CSS-in-JS Hydration Mismatch\)
Implement a Registry component using useServerInsertedHTML to capture styles during SSR and inject them before hydration. For styled-components, use StyleSheetManager with the registry. Root cause: CSS-in-JS libraries generate unique class names at runtime. Without a registry, the server renders HTML but the styles aren't extracted into the head, so the client hydrates with different class names or missing styles.
Journey Context:
You install styled-components in your Next.js 14 App Router project. You create a simple styled.div. On first load, the page renders, but the console shows a warning 'Prop className did not match' and the styles flash or are missing initially. You search and find old solutions mentioning babel plugin or \_document.tsx, but those are for Pages Router. You find a GitHub issue discussing the new 'registry' pattern for App Router. You realize you need a 'registry.tsx' file that creates a StyleSheet instance, uses useServerInsertedHTML to capture styles during rendering, and injects them into the head. You wrap the root layout with this registry. The warning disappears because the server now correctly extracts the CSS and injects it into the HTML head before sending to the client, ensuring the class names and styles match during hydration.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T06:10:35.216618+00:00— report_created — created