Report #62547
[bug\_fix] There was an error while hydrating. Because the error happened outside of Suspense, the entire root will switch to client rendering. \(Styled-components FOUC\)
Create a Registry component that uses styled-components' ServerStyleSheet and next/navigation's useServerInsertedHTML to collect styles during SSR and inject them into the . Mark the registry with 'use client' and wrap the app with it in layout.tsx.
Journey Context:
You integrate styled-components into a Next.js 14 App Router project. You install the package, create a simple \`const Box = styled.div\`..., and render it in a page. In development \(\`next dev\`\), styles appear correctly. You run \`next build && next start\` for production testing. On load, the page shows unstyled HTML for a flash \(FOUC - Flash of Unstyled Content\), then styles apply. The console shows hydration errors because the server-rendered HTML doesn't contain the styled-components CSS classes in the head. You search and find that styled-components generates unique class names and CSS during render, but in App Router, you need to manually extract these styles during SSR and inject them. You create a \`registry.tsx\` file that imports \`ServerStyleSheet\` from styled-components and \`useServerInsertedHTML\` from \`next/navigation\`. In the registry, you use \`useState\` to create a sheet instance, then in \`useServerInsertedHTML\`, you check if there are styles and return a style tag with the CSS. You mark \`registry.tsx\` with 'use client' \(to use hooks\) but since it's imported by the root layout \(Server Component\), the styles are collected during SSR and injected into the HTML head. You wrap the children in layout.tsx with the Registry. After rebuilding, the FOUC disappears and hydration errors are gone because the server now sends the CSS in the initial HTML payload.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T11:28:09.152849+00:00— report_created — created