Skip to content

6.4.x silently drops most rules in production browser bundles when process is not a runtime global #5738

Closed

Description

@vlindhol

TL;DR

In 6.4.0/6.4.1, a refactor of the DISABLE_SPEEDY constant in packages/styled-components/src/constants.ts added a typeof process !== 'undefined' guard around the NODE_ENV !== 'production' fallback. In browser bundles produced by Vite, rolldown, esbuild, etc., process.env.NODE_ENV is statically replaced with "production" but process itself is not provided as a runtime global. The new guard fails at runtime, the fallback returns true, and DISABLE_SPEEDY becomes true in production.

The consequence is that useCSSOMInjection is false, every styled component is injected via the legacy TextTag path (raw <style> child text nodes), and once an app has more than a handful of styled components the browser's CSS parser stops keeping up with incremental text-node mutations — the bulk of the rules end up in the <style> element's textContent but never make it into sheet.cssRules. Visually, most styles are missing in production while dev mode is unaffected.

Workaround: configure the bundler to define process (or at least process.env.NODE_ENV) at runtime, or stay on 6.3.x.

Affected versions

  • 6.4.0
  • 6.4.1

6.3.12 is unaffected.

Affected setups

Any browser bundle where:

  • The bundler statically replaces process.env.NODE_ENV with a string literal (Vite, rolldown, esbuild, modern Webpack with DefinePlugin, etc.), and
  • process is not otherwise polyfilled into the browser globals (the default for Vite SPAs).

This is the most common modern SPA setup, so a wide swath of users is exposed.

Symptom

In a production build:

  • The page renders with most components unstyled (default browser styles).
  • The <style data-styled> element exists in <head> and contains a large textContent (all the user-app CSS is there as text).
  • document.querySelector('style[data-styled]').sheet.cssRules.length is much smaller than the number of unique styled components on the page — typically just the first few rules that were inserted before the element was committed to the DOM.

In dev mode the same app renders correctly — process.env.NODE_ENV is "development" so DISABLE_SPEEDY is true regardless of the new guard, but dev mode tolerates the slow path because the rule volume is lower and HMR forces frequent re-parses.

Reproduction

Any production-mode Vite/rolldown/esbuild SPA with:

  • styled-components 6.4.0 or 6.4.1.
  • More than ~5–10 styled components rendered on first paint.
  • No explicit define for process in the bundler config.
  • A <StyleSheetManager> (or no manager — same outcome since the regression is in mainSheet's defaults, not the manager).

Empirically, enabling enableVendorPrefixes makes the failure mode much more reliable because the per-rule text grows large enough to push past whatever browser threshold matters; with prefixes off the same app may appear to work because the smaller text fits in the parser's first-batch budget. Both configurations are on the broken code path.

Root cause — line-level diff

packages/styled-components/src/constants.ts:

6.3.12 (works):

export const DISABLE_SPEEDY = Boolean(
  typeof SC_DISABLE_SPEEDY === 'boolean'
    ? SC_DISABLE_SPEEDY
    : /* env-var checks */
        : process.env.NODE_ENV !== 'production'
);

The bare process.env.NODE_ENV reference relies on bundlers statically replacing it with "production" or "development". In a production browser bundle this collapses to "production" !== "production" → false → DISABLE_SPEEDY = false → useCSSOMInjection = true → CSSOMTag.

6.4.0 / 6.4.1 (broken):

function readSpeedyFlag(name: string): boolean | undefined {
  if (typeof process !== 'undefined' && typeof process.env !== 'undefined') {
    const val = process.env[name];
    if (val !== undefined && val !== '') return val !== 'false';
  }
  return undefined;
}

export const DISABLE_SPEEDY = Boolean(
  typeof SC_DISABLE_SPEEDY === 'boolean'
    ? SC_DISABLE_SPEEDY
    : (readSpeedyFlag('REACT_APP_SC_DISABLE_SPEEDY') ??
       readSpeedyFlag('SC_DISABLE_SPEEDY') ??
       (typeof process !== 'undefined' && typeof process.env !== 'undefined'
          ? process.env.NODE_ENV !== 'production'
          : true))
);

The new typeof process !== 'undefined' guard runs at runtime — bundlers can't statically replace typeof process (it would change behaviour for legitimate typeof checks in user code). At runtime in a Vite/rolldown/esbuild browser bundle, process is not declared, so:

  • typeof process → 'undefined'
  • The guarded ternary evaluates the : true branch
  • DISABLE_SPEEDY = Boolean(true) = true
  • mainSheet's useCSSOMInjection is false
  • makeTag() returns a TextTag instead of a CSSOMTag

The two readSpeedyFlag lookups before this don't help — they correctly fall back to undefined when process is absent, which is exactly when the third fallback fires.

Why the browser stops at the first few rules

TextTag injects each rule by appending a fresh text node child to the live <style> element:

this.element.insertBefore(node, refNode || null);

Browsers treat a <style> element's CSS as the concatenation of its child text. The HTML/CSS spec requires sheet.cssRules to track changes, but engine implementations are inconsistent about re-parsing incremental child mutations after the element is committed to the DOM. In practice, browsers parse the first batch of text nodes that arrive together and silently drop further mutations on the floor (or process them lazily/never). This is exactly why CSSOMTag (CSSStyleSheet.insertRule) exists — it bypasses the HTML parser and writes directly to the CSSOM, which is well-defined for incremental updates.

TextTag was historically the dev-only / SSR-rehydration path. It was not battle-tested as the production injection path for non-trivial apps. Apps that previously hit it on purpose (via disableCSSOMInjection or SC_DISABLE_SPEEDY=true) tend to be small enough that the failure mode doesn't surface.

Suggested fix

In constants.ts, restore the 6.3.12 behaviour for the NODE_ENV fallback. Either drop the guard:

: process.env.NODE_ENV !== 'production'

…and trust the bundler's static process.env.NODE_ENV replacement (the same approach 6.3.12 used and that the rest of the codebase still relies on, e.g. in ComponentStyle.ts).

…or invert the default so the guard fails closed instead of open:

: (readSpeedyFlag('REACT_APP_SC_DISABLE_SPEEDY') ??
   readSpeedyFlag('SC_DISABLE_SPEEDY') ??
   (typeof process !== 'undefined' && typeof process.env !== 'undefined'
      ? process.env.NODE_ENV !== 'production'
      : false))   // ← was `true`

If process is absent, we are by definition in some kind of bundled/browser-like environment, where the speedy path is the right default. Returning true (slow path) in that case is the wrong way to be defensive.

The two readSpeedyFlag env-var lookups can keep the guard — those are genuinely opt-in and correctly returning undefined when process is missing.

Workarounds

In priority order:

  1. Stay on 6.3.12.
  2. Define process.env.NODE_ENV (and ideally a minimal process) in your bundler config. For Vite:
    // vite.config.ts
    define: {
      'process.env.NODE_ENV': JSON.stringify(mode),
      // optional, makes the typeof guard pass:
      'globalThis.process': '({ env: { NODE_ENV: "production" } })',
    }
  3. <StyleSheetManager enableVendorPrefixes={false}> if your target browsers don't need prefixes. This is not a fix — it just makes the per-rule text small enough to fit under the browser's first-batch parsing budget. Apps with enough total CSS will still fail.

Activity

  1. self-assigned this
    on May 14, 2026
  2. added a commit that references this issue on May 14, 2026
    9945904
  3. quantizor commented on May 14, 2026

    @quantizor
    Contributor

    Could you try [email protected] when you get a chance?

  4. quantizor commented on May 19, 2026

    @quantizor
    Contributor

    Fix released with 6.4.2

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