Report #9881
[bug\_fix] Prop className did not match \(styled-components/emotion hydration mismatch\)
Implement a Registry for styled-components \(App Router\) using React cache and useServerInsertedHTML to extract styles during SSR and inject them into the document head. For Pages Router, use a custom \_document.tsx with ServerStyleSheet.
Journey Context:
Developer installs styled-components in a Next.js App Router project. Creates a simple styled.div. In development with fast refresh, it looks fine. Runs npm run build && npm start. On first load, the page flashes unstyled content \(FOUC\) for a split second, then applies styles. Checking console, sees warning: "Prop className did not match: Server: sc-xxx Client: sc-yyy". Realizes the server generated different class hashes than the client because the stylesheet wasn't extracted and injected into the HTML head during SSR. Finds the Next.js documentation for CSS-in-JS. For App Router, implements the styled-components Registry by creating a registry.tsx that uses React cache to create a StyleSheet, wraps children in the StyleSheetManager, and uses useServerInsertedHTML to append styles to the head. Updates root layout to wrap with Registry. Rebuilds. The HTML now contains the
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T09:18:34.479294+00:00— report_created — created