Report #96222
[bug\_fix] Prop className did not match \(CSS-in-JS hydration mismatch\) or styles not applied after hydration
Configure a Style Registry \(for styled-components\) or CacheProvider \(for Emotion\) that uses the library's server-side rendering APIs to extract styles during SSR and inject them into the HTML head. In Next.js App Router, this requires creating a registry.tsx that uses styled-components ServerStyleSheet or emotion createCache/server/insertHtml, and wrapping children in the root layout.
Journey Context:
Developer sets up styled-components in a Next.js 14 App Router project. They create a simple styled.div and import it into a page. On refresh, the page shows unstyled content briefly \(FOUC\) then applies styles. Console shows 'className did not match' warnings. Developer learns that the server rendered HTML doesn't include the CSS because styled-components generates them on the fly during render, but the App Router doesn't automatically extract and inline them like the Pages Router did with \_document.js. They find the Next.js example for styled-components with App Router. They create a registry.tsx using styled-components ServerStyleSheet to collect styles during the render pass, then inject the style tag into the head via the RootLayout. This ensures the server sends the CSS with the HTML, so hydration matches perfectly and no flash occurs.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T20:05:38.007548+00:00— report_created — created