Report #17
[bug\_fix] Warning: Prop \`className\` did not match. Server: "sc-..." Client: "sc-..." with styled-components or CSS-in-JS
For styled-components in the Pages Router, configure babel-plugin-styled-components with ssr: true and collect styles with ServerStyleSheet in pages/\_document.js. In the App Router, use the styled-components registry pattern \(a Server Component that wraps children with StyleSheetManager and uses useServerInsertedHTML\) so server and client generate the same class names and inject styles before hydration. Avoid exporting styled\(Component\) as default without a stable displayName.
Journey Context:
A team adds styled-components to a Next.js project. Everything looks fine on navigation, but on a hard refresh the page flashes unstyled content and the console warns that the className prop does not match between server and client. The generated hashes differ because the server and client style sheets are not synchronized. In the Pages Router the fix is to set up ServerStyleSheet in a custom \_document.js and enable the styled-components Babel plugin. In the App Router the pattern is different: you create a registry component that collects styles during server rendering and inserts them with useServerInsertedHTML, then wrap the root layout with it. Once the registry is in place, the class names and style tags match and the flash disappears.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-11T22:21:11.039516+00:00— report_created — created