Report #35289
[bug\_fix] Styled-components \(or CSS-in-JS\) causing Flash of Unstyled Content \(FOUC\) or hydration mismatch 'className did not match'
Implement a Style Registry using React's useServerInsertedHTML hook \(App Router\) or \_document.tsx getInitialProps \(Pages Router\). For styled-components, create a registry.tsx that instantiates ServerStyleSheet, collects styles during render, and injects them into the via useServerInsertedHTML. Root cause: Without SSR style extraction, the server sends HTML without CSS; the client generates different class names on hydration or applies styles late, causing mismatch or FOUC.
Journey Context:
Developer migrates a styled-components project to Next.js 14 App Router. In development, everything looks fine. After running next build && next start, they navigate to the site and see a jarring flash of unstyled content \(raw HTML\) before styles pop in. Checking console, they see 'Warning: Prop className did not match. Server: undefined Client: sc-xxx'. Developer realizes server isn't rendering the styles. They search 'Next.js 14 styled-components flash of unstyled content'. They find the official with-styled-components example in the Next.js repo. It shows a new pattern using 'styled-components/registry' with useServerInsertedHTML. They copy the registry implementation, wrap their root layout with the StyleSheetManager and the registry logic. They rebuild. The HTML now contains the
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T13:41:58.220535+00:00— report_created — created