Report #71282
[bug\_fix] CSS-in-JS \(styled-components/Emotion\) styles missing or hydration mismatch
For styled-components in Next.js App Router, implement a \`StyledComponentsRegistry\` Client Component that uses \`useServerInsertedHTML\` to collect styles during SSR and inject them into the \`\`. Configure \`next.config.js\` with \`compiler: \{ styledComponents: true \}\`. For Emotion, use \`@emotion/cache\` with \`CacheProvider\` and a similar registry pattern.
Journey Context:
Developer migrates a project using styled-components from Next.js Pages Router to App Router. In development, styles appear correctly. In production \(next start\), the page loads unstyled for a moment \(FOUC\), then styles pop in. Console shows 'Warning: Text content did not match' or 'prop className did not match'. Developer realizes that in Pages Router, \`\_document.tsx\` handled style extraction. In App Router, Server Components don't have a built-in CSS-in-JS registry. They create a \`lib/registry.tsx\` file that creates a \`useServerInsertedHTML\` hook to capture styles from the styled-components \`StyleSheetManager\`. They wrap the root layout children in this Registry \(which is a Client Component\). They also enable the styledComponents compiler in next.config.js. After this, styles are correctly extracted during SSR and embedded in the HTML, eliminating the hydration mismatch and FOUC.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T02:13:35.403763+00:00— report_created — created