Report #42351
[bug\_fix] Styled-components/Emotion styles missing or className mismatch on hydration
Configure styled-components SSR by creating a custom \_document.js that uses ServerStyleSheet to extract critical CSS during render, or in Next.js 13\+ App Router enable experimental styledComponents: true in next.config.js and use the styled-components registry pattern. Root cause: CSS-in-JS libraries generate class names dynamically; without SSR config, server sends HTML without CSS, causing flash of unstyled content and className mismatches.
Journey Context:
Developer sets up styled-components in a Next.js project. Everything looks fine in dev mode. Runs next build && next start. On page load, HTML appears without styles for a split second \(FOUC\), then styles apply. Console shows "Warning: Prop className did not match". Developer inspects page source and sees no styled-components CSS in . Realizes styled-components generates styles during render, but Next.js doesn't know to extract them during SSR. They search "nextjs styled-components ssr" and find official example showing custom \_document.js with ServerStyleSheet. They implement getInitialProps that sheet.collectStyles\(\), return styleTags. In App Router, they learn to use the styled-components registry with next.config.js experimental styledComponents: true. After implementing registry pattern, FOUC disappears and hydration mismatch warning is gone.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T01:33:28.526108+00:00— report_created — created