Skip to content

SSR hydration mismatch (all class names differ between server and client) introduced in 6.3.12 with webpack + nodeExternals #5737

Done

Description

@zdila

Description

After upgrading from 6.3.11 to 6.3.12 (and all later versions including 6.4.x), every single styled-component generates a different class name on the server vs. the client, causing React to report hydration mismatches for the entire component tree. Downgrading back to 6.3.11 resolves the issue.

Environment

Package Version
styled-components 6.3.12, 6.4.0, 6.4.1 (broken), 6.3.11 (works)
babel-plugin-styled-components 2.1.4
react 19.2.5
webpack 5.106.2
Node.js 24.14.0

Setup

  • Server bundle: webpack with target: 'node' + webpack-node-externals. The server does not bundle styled-components — it require()s it at runtime from node_modules, loading dist/styled-components.cjs.js.
  • Client bundle: webpack with target: 'web'. webpack resolves the browser field in styled-components' package.json and bundles dist/styled-components.browser.cjs.js.
  • Server-side rendering uses ServerStyleSheet + collectStyles / getStyleTags().
  • babel-plugin-styled-components configured with { ssr: true }.

Symptom

In development mode, React throws hydration errors for every styled-component in the tree. Example:

Hydration failed because the server rendered HTML didn't match the client.

<Animation__StyledAnimation>
  <div
+   className="Animation__StyledAnimation-sc-14yz3tx-0 gCnCGD"  ← client
-   className="Animation__StyledAnimation-sc-14yz3tx-0 fajRrh"  ← server
  >

The component ID (sc-14yz3tx-0) matches on both sides — confirming babel-plugin-styled-components is generating stable IDs. Only the dynamic class hash suffix differs, for every component including those with fully static CSS (no dynamic interpolations).

In production mode (process.env.NODE_ENV = 'production') the error is silently swallowed by React, but the mismatch still exists (potential flash of wrong styles).

Root cause hypothesis

The server loads dist/styled-components.cjs.js at runtime (via Node.js require), while webpack bundles dist/styled-components.browser.cjs.js for the client (via the browser field in package.json). These two files have different initialization code. Something changed between 6.3.11 and 6.3.12 in one or both of these files that causes the internal class name hashing to diverge between the two separate module instances.

Reproduction steps

  1. Use webpack with webpack-node-externals for SSR (server does not bundle styled-components).
  2. Use webpack target: 'web' for the client bundle (webpack picks up the browser field → styled-components.browser.cjs.js).
  3. Use ServerStyleSheet / collectStyles / getStyleTags() for SSR.
  4. Upgrade from [email protected] to 6.3.12 or later.
  5. Visit any SSR-rendered page in a browser with devtools open in development mode.

Expected: no hydration warnings, class names match between server and client.
Actual: hydration mismatch on every styled-component in the tree.

Workaround

Pin to [email protected].

Activity

  1. quantizor commented on May 14, 2026

    @quantizor
    Contributor

    I haven't been able to verify this one locally yet. A reproduction would be fantastic if possible.

  2. self-assigned this
    on May 14, 2026
  3. BiscuiTech commented on Jun 9, 2026

    @BiscuiTech

    I was also able to reproduce this with an Astro (vite), React & SC setup.

    I'm working on simplifying the reproduction repo.

  4. josephspandrusyszyn-bstock commented on Jul 3, 2026

    @josephspandrusyszyn-bstock

    I ran into a similar problem, and was able to track it down to the root cause being a difference in enableVendorPrefixes.

    We're running Next.js, and were following Next's recommendations for style registries (except with enableVendorPrefixes enabled): https://nextjs.org/docs/app/guides/css-in-js#styled-components

    We fixed the issue on our end by changing this:

      if (typeof window !== 'undefined') return <>{children}</>
     
      return (
        <StyleSheetManager sheet={styledComponentsStyleSheet.instance} enableVendorPrefixes>
          {children}
        </StyleSheetManager>
      )
    

    ... into this:

      return (
        <StyleSheetManager sheet={typeof window === 'undefined' ? styledComponentsStyleSheet.instance : undefined} enableVendorPrefixes>
          {children}
        </StyleSheetManager>
      )
    

    This of course may or may not be the same root cause as what the others reporting the problem were seeing since we're using Next and they weren't, but the symptoms were the same.

  5. quantizor commented on Nov 25, 2026

    @quantizor
    Contributor

    Fixed in 6.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions