Skip to content

fix: prevent createGlobalStyle style accumulation in jsdom tests - #5699

Merged
quantizor merged 3 commits into
mainfrom
fix/global-style-jsdom-perf
Mar 21, 2026
Merged

quantizor merged 3 commits into
mainfrom
fix/global-style-jsdom-perf

Conversation

@quantizor

Copy link
Copy Markdown
Contributor

Summary

  • Removes __SERVER__ from the server-side render guard in createGlobalStyle, gating only on IS_RSC || styleSheet.server
  • Fixes O(n²) performance degradation in jsdom test environments where the server build's dead-code elimination removes useLayoutEffect cleanup but still renders styles unconditionally

Root cause: Jest resolves the main CJS entry (server build) where __SERVER__=true. This eliminates the useLayoutEffect cleanup path but unconditionally triggers style injection during the component body. In jsdom, CSS rules accumulate in the <style> element (~301 nodes per cycle) without ever being removed, causing each subsequent render to be progressively slower due to jsdom's O(n) insertBefore.

Why this is safe: On a real server without ServerStyleSheet, styles go into VirtualTag (array-backed, never flushed to HTML) and are discarded anyway. The Turbopack SSR case (where __SERVER__ is false on the server) is already handled by the styleSheet.server runtime fallback.

Benchmark (reporter's repro, 20 render/unmount cycles with 300-rule GlobalStyle):

Build Before After
Current (main) 65,000ms (O(n²): 344ms→6,276ms/cycle) 173ms (flat ~6ms/cycle)

Fixes #5674

…onments

The server build replaces __SERVER__ with true at build time, which
dead-code eliminates the useLayoutEffect cleanup. But it also
unconditionally triggers style rendering during the component body.
In jsdom (where IS_BROWSER is true), this causes CSS rules to
accumulate in the style element without cleanup on each render/unmount
cycle, producing O(n²) performance degradation.

Remove __SERVER__ from the server render guard — gate only on IS_RSC
or styleSheet.server. On a real server without ServerStyleSheet, styles
go into VirtualTag and are discarded anyway; styleSheet.server handles
the Turbopack SSR case at runtime.

Fixes #5674
@changeset-bot

changeset-bot Bot commented Mar 21, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0a255c7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
styled-components Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@quantizor
quantizor marked this pull request as ready for review March 21, 2026 14:57
@quantizor
quantizor requested a review from Copilot March 21, 2026 14:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a styled-components v6.3.x performance regression in jsdom/Jest environments by preventing createGlobalStyle from injecting styles during render when the “server” build is accidentally used in the browser-like test runtime, which previously led to unbounded <style> node accumulation and O(n²) slowdowns.

Changes:

  • Adjusts the SSR/render-phase injection guard in createGlobalStyle to no longer rely on __SERVER__, and instead gate on IS_RSC or styleSheet.server.
  • Adds a patch changeset describing the jsdom performance regression fix.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
packages/styled-components/src/constructors/createGlobalStyle.ts Changes the render-time injection condition to avoid style accumulation in jsdom when cleanup effects are eliminated.
.changeset/fix-global-style-jsdom-perf.md Records the patch-level release note for the regression fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@quantizor
quantizor merged commit 0507ace into main Mar 21, 2026
4 checks passed
@quantizor
quantizor deleted the fix/global-style-jsdom-perf branch March 21, 2026 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance degradation with tests and 6.3.x

2 participants