Repository navigation
Description
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_ENVwith a string literal (Vite, rolldown, esbuild, modern Webpack withDefinePlugin, etc.), and processis 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 largetextContent(all the user-app CSS is there as text). document.querySelector('style[data-styled]').sheet.cssRules.lengthis 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-components6.4.0 or 6.4.1.- More than ~5–10 styled components rendered on first paint.
- No explicit
defineforprocessin the bundler config. - A
<StyleSheetManager>(or no manager — same outcome since the regression is inmainSheet'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
: truebranch DISABLE_SPEEDY = Boolean(true) = truemainSheet'suseCSSOMInjectionisfalsemakeTag()returns aTextTaginstead of aCSSOMTag
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:
- Stay on 6.3.12.
- Define
process.env.NODE_ENV(and ideally a minimalprocess) 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" } })', }
<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
6.x bugfixes (#5746)
quantizor commentedon May 14, 2026
Could you try
[email protected]when you get a chance?quantizor commentedon May 19, 2026
Fix released with 6.4.2