Report #10458
[bug\_fix] Styled-components \(or CSS-in-JS\) hydration mismatch - className mismatch
Implement a Styled Components Registry using \`react\` APIs to collect styles during SSR and inject them into the HTML head, and ensure \`compiler.styledComponents\` is enabled in \`next.config.js\` to handle the Babel plugin correctly.
Journey Context:
Developer sets up Next.js 14 App Router with styled-components. They create a simple styled button and render it in a page. In development, they see a warning: 'Warning: Prop \`className\` did not match. Server: "sc-bdnxRM" Client: "sc-ktHwxA"'. The styles also appear broken or flash unstyled content \(FOUC\) before applying. Developer searches and finds conflicting advice about Babel plugins. They try creating a \`.babelrc\` but Next.js 14 uses SWC by default. They realize styled-components generates different class names on server vs client because the Server Style Sheet isn't being extracted and passed to the client. The fix requires creating a \`lib/registry.tsx\` that uses \`useServerInsertedHTML\` \(a React API for App Router\) to collect all styles during SSR and inject them into the document head. Additionally, they must enable \`compiler: \{ styledComponents: true \}\` in next.config.js to ensure the SWC compiler handles the styled-components display names and SSR correctly. This ensures both server and client generate the same class names and styles are present in the initial HTML.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T10:46:18.585941+00:00— report_created — created