Repository navigation
fix: prevent createGlobalStyle style accumulation in jsdom tests - #5699
Conversation
…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 detectedLatest commit: 0a255c7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
There was a problem hiding this comment.
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
createGlobalStyleto no longer rely on__SERVER__, and instead gate onIS_RSCorstyleSheet.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.
Summary
__SERVER__from the server-side render guard increateGlobalStyle, gating only onIS_RSC || styleSheet.serveruseLayoutEffectcleanup but still renders styles unconditionallyRoot cause: Jest resolves the
mainCJS entry (server build) where__SERVER__=true. This eliminates theuseLayoutEffectcleanup 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 intoVirtualTag(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 thestyleSheet.serverruntime fallback.Benchmark (reporter's repro, 20 render/unmount cycles with 300-rule GlobalStyle):
main)Fixes #5674