Repository navigation
fix(createGlobalStyle): keep each global style's CSS when two differ only in their functions - #5846
Conversation
🦋 Changeset detectedLatest commit: a662b14 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.
ℹ️ No blocking issues — a couple of doc-accuracy notes inline and below.
Reviewed changes
The PR fixes #5845 by giving function-bodied global styles distinct component ids, so a global whose rules are only function interpolations no longer collapses onto the id of another.
- New id helper —
packages/styled-components/src/utils/globalStyleId.ts:globalStyleComponentIdhashes static rules from their JSON (order-independent, unchanged); for rules containing a function, the first occurrence of a serialization keeps the plain hash and later occurrences mix a per-serialization count into the hash.containsFunctionwalks rules recursively, treating styled components and keyframes values as function-free since they carry their own id in JSON. - Wiring —
createGlobalStyle.tsnow calls the helper instead ofgenerateComponentId(JSON.stringify(rules)). - Tests — new unit suite for the helper, a client/SSR shape matrix and dedup cases in
createGlobalStyle.test.tsx, and RSC id cases. The function-body count is reset between cases insrc/test/utils.tsandtest-utils/setupTestFramework.ts.
I read the full diff, traced the helper through WebGlobalStyle/Sheet, and confirmed the reported repro (including the () => css\...`` form) is fixed. Full web suite (1558 tests), typecheck, and Biome all pass.
Technical details
# id scheme
## Behavior confirmed
- Issue #5845 repro and the `createGlobalStyle(() => css`...`)` variant now
render both rules on the client and in SSR.
- Styled components are memo-shaped objects carrying `styledComponentId`, so
`JSON.stringify` includes the id and `isStyledComponent` correctly short
circuits `containsFunction`. Keyframes are class instances carrying `name`.
- Swapping the module-eval order of two same-serialization function-bodied
globals swaps their ids (order-dependent, documented).
## Not re-flagged
- The order-dependence itself is stated in the changeset and PR body; no fix
proposed.deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
|
Two non-anchored doc-accuracy notes that did not fit an inline anchor:
An unreleased changeset still promises unconditional SSR/client byte-stability.
|
There was a problem hiding this comment.
ℹ️ No blocking issues — the salting change is correct and well covered; two small notes inline.
Reviewed changes
The three commits since the prior pullfrog review harden the function-bodied id scheme and correct the changelog wording.
- Salts every function-bodied id —
globalStyleComponentIdnow mixes the per-serialization count into every function-bodied id, including the first, so a function body can no longer share the id of a staticInfinity/NaNslot that serializes to the same JSON. JSDoc updated for the order-dependent scheme. - Scopes the changelog — both changesets now restrict the dedup claim to static globals and document the order/cascade caveat.
- Drops the duplicate reset —
resetStyledno longer callsresetGlobalStyleComponentIds(the Jest setup hook already restarts the count) and new client/RSC tests cover the Infinity/NaN collision, creation-order cascade, shared-group unmount, and per-instance injection.
Verified locally: full web suite (1566), native suite (2546), typecheck, and Biome all pass. I confirmed the setup reset is still load-bearing by disabling it — the RSC snapshot ids drift without it.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
228bb82 to
a405fbd
Compare
There was a problem hiding this comment.
ℹ️ No critical issues — one edge-case regression inline.
Reviewed changes
The branch was rewritten since the prior pullfrog review (which reviewed the earlier globalStyleId.ts id-salting scheme). It abandons changing ids entirely and instead keeps colliding ids while moving the per-instance rule cache to a module-global registry.
- Module-global registry — replaced the per-
WebGlobalStyleinstanceRulesmap withregistry: Map<componentId, Map<instance, {name, rules}>>, and rewroterebuildGroupto re-insert every surviving entry for an id across all global styles that share it. - Render lifecycle — added a server-only append path and a static fast path in
WebGlobalStyle.renderStyles; HMR cleanup now routes throughremoveStylesinstead ofclearRules. - Nested
cssdetection —isStaticRulesrecurses into nestedcss-helper arrays, so a global whose only function sits inside a nested helper is now dynamic and updates on prop changes. - Removed the id helper — deleted
utils/globalStyleId.tsand its unit suite, dropped thesetupTestFrameworkcount reset, and broadened the client/SSR shape matrix with a hydration round-trip test and anisStaticRulestable. - Bundle ceiling — raised the treeshake byte ceilings by 0.1KB for the registry.
Read the full diff, traced the single-sheet mount/update/unmount, rehydration, HMR, StyleSheet-swap, and RSC paths against Sheet.clearRules/insertRules/hasNameForId, and confirmed the full web suite (1582), RSC suite (25), and the createGlobalStyle file (98) are green. Prior review threads are resolved and no longer in the diff. The one new concern is the registry's lack of sheet identity, detailed inline.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
The one commit since the prior pullfrog review makes the global-style instance registry sheet-aware, closing the cross-sheet bleed that review flagged.
- Sheet-aware registry —
InstanceEntrynow records theStyleSheetit was computed for, andrebuildGroupskips entries whose sheet is not the calling sheet, so a same-id global rendered on another sheet no longer has its rules inserted into this one. - Isolation test —
keeps same-id globals on different sheets out of each othermounts two same-id function-bodied globals on separate sheets and asserts each sheet keeps only its own rules through mount and unmount.
Prior review threads are resolved. Verified locally: full web suite (1583) and the createGlobalStyle file (99) pass.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
40c0bf4 to
2579cf5
Compare
There was a problem hiding this comment.
Important
One new finding: the server append guard can drop a sibling global's CSS when one instance re-renders into the same collection.
Reviewed changes
The branch was restructured again since the prior pullfrog review at 40c0bf4; the net changes are the sheet-keyed registry, the unified rebuild, and the server append guard.
- Sheet-keyed registry —
WebGlobalStylenow stores instance rules inWeakMap<StyleSheet, Map<componentId, Map<instance, { name, rules }>>>; the owning sheet moved out of each entry and into the registry key, andgetInstance/deleteInstance/liveInstanceCounttake the sheet. - Unified group rebuild —
rebuildGroup(sheet, instances)clears the group and re-inserts surviving entries in map (mount) order, deduping static entries byentry.name === id;removeStylesrebuilds from the post-delete map. - Server append guard — a dynamic instance on a server sheet appends only when its name is not already registered; a repeat render rebuilds instead (one finding inline).
- Docs and tests — new
docs/global-styles.md, anAGENTS.mdpointer, a broad client/SSR shape matrix, anisStaticRulesnested-csstable, and an SSR round-trip hydration test.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
The commit since the prior pullfrog review keeps server-sheet registry entries alive so a repeat render can rebuild from every sibling, closing the server append finding raised at 2579cf5.
- Retained server entries — removed the per-render server cleanup in
createGlobalStyle.ts, so a server sheet's instance entries survive until the sheet is collected.renderStylesnow appends only on an instance's first render (styleSheet.server && prev === undefined) and rebuilds on a repeat, which restores the sibling entries aclearRuleswould otherwise drop. - RSC entry release — the RSC branch deletes its registry entry whenever one exists rather than only when the compiled CSS is non-empty, so an empty-rule mount no longer leaks into the process-wide default sheet.
- Docs and tests —
docs/global-styles.md"Server" rewritten for the retained-entry model; the single-instance repeat-render test now asserts sibling preservation, and a new test rerenders one of two mounts against a server sheet.
Verified locally at 3265b61: createGlobalStyle.test.tsx (104 tests), createGlobalStyle.rsc.test.tsx + ssr.test.tsx (71 tests) all pass.
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏
3265b61 to
e7186fa
Compare
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
One commit landed since the prior pullfrog review: a rebase onto main plus build-surface repairs for main's new property-mangling guard.
- Restored
@internaltags —isStatic, the privatecomputeRules, and the privaterebuildGroupinWebGlobalStylecarry@internalagain, keeping them out of the emitted.d.tsthatdist-behavior.test.jsscans against the mangle list. - Dropped the dead mangle entry —
instanceRulesleftscripts/internalPropertyNames.cjstogether with the field it named. - Adjusted the size ceiling — the webpack treeshake limit is 13.35kB against a measured 13.30kB, with the comment naming the registry as the cost.
- Rebased onto main — carries main's bundle-shrink and mangling work with no reverted files or conflict residue.
Verified at e7186fa5: full web suite (1679 tests) and pnpm build + test:build (117 tests, including dist-behavior.test.js and treeshake.test.ts) all pass.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
One commit landed since the prior pullfrog review: a test-only rewrite of the SSR hydration ordering test's server-tag seeding.
- DOM-built server tag — the test now takes
ServerStyleSheet#getStyleElement()'s props, creates a<style>withcreateElement/setAttribute, and assigns the escaped CSS viatextContent, replacingdocument.head.innerHTML = sheet.getStyleTags(). Both emissions use adata-styledvalue other thanSC_ATTR_ACTIVE(truevs empty string), sorehydrateSheettreats the reconstructed tag exactly as the parsed one; the existinggetStyleTags()content assertions remain.
No production code changed. Verified at 81c46f9: createGlobalStyle.test.tsx (105 tests), pnpm typecheck, and Biome on the changed file all pass.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏
…only in their functions
81c46f9 to
a662b14
Compare
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
The branch was rebased onto main since the prior pullfrog review; the only net branch-authored edit is in the bundle-size test.
- Rebased onto
1fef5195— carries main's fixed-length version measurement and the recent parser commits, with the PR's production files unchanged from81c46f9. - Raised size ceilings — the standalone minified ceiling moves 14.6 → 14.65kB and the webpack ceiling 13.4 → 13.5kB; the webpack comment now names the global-style instance registry as the cost. Re-measured at
a662b14with the test's fixed-length method: 14.63kB and 13.45kB, both under their ceilings.
deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

Two
createGlobalStylecalls whose templates differ only in their interpolated functions, such as xstyled's Preflight and ColorModeProvider, could wipe each other's CSS. Mounting, rerendering, or unmounting one of them removed the other's rules from the page.What changes
createGlobalStyle`${() => 'html { --a: 1; }'}`).csshelper renders its CSS and updates when its props change.Class names and global style ids are unchanged, so server-rendered pages hydrate as before.
Cost
Bundle size: 13,777 bytes (webpack, gzip) and 14,986 bytes (minified, gzip), against 13,700 and 14,918 on
main. The bundle ceilings in the size test rise to 13.5 kB and 14.65 kB to carry this.