Report #57216
[bug\_fix] Hydration failed due to className mismatch \(CSS-in-JS\)
For Next.js App Router, implement a \`styled-components\` registry using \`useServerInsertedHTML\` to collect styles during SSR and inject them into the HTML. For Pages Router, customize \`\_document.js\` to extract and render the ServerStyleSheet.
Journey Context:
Developer sets up \`styled-components\` in their Next.js project. In development, everything looks fine, but on production build \(\`next build\`\), they see a hydration mismatch warning about \`className\` not matching \(e.g., server: "sc-abc123", client: "sc-def456"\), or styles are missing entirely on initial load \(flash of unstyled content\). Developer dives into the styled-components docs and Next.js examples. They learn that styled-components generates unique class names and CSS dynamically. On the server, if the CSS isn't extracted and inlined into the HTML \`\`, the client renders HTML without styles, then generates different class names on hydration. The fix involves creating a 'registry' component in the App Router that uses \`useServerInsertedHTML\` to collect all styles generated during the server render and inject them before the HTML is sent.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T02:31:33.940060+00:00— report_created — created