Skip to content

fix(createGlobalStyle): keep each global style's CSS when two differ only in their functions - #5846

Merged
quantizor merged 1 commit into
mainfrom
cursor/global-style-function-id-4915
Nov 4, 2026
Merged

quantizor merged 1 commit into
mainfrom
cursor/global-style-function-id-4915

Conversation

@quantizor

@quantizor quantizor commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Two createGlobalStyle calls 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

  • Each global style keeps its own CSS when two of them differ only in what their functions return, including bodies made of a single function (createGlobalStyle`${() => 'html { --a: 1; }'}`).
  • When two such global styles set the same property, the one mounted most recently wins. A prop or theme update doesn't change which one that is.
  • Server rendering emits the CSS of every such global style, and of every mount of one dynamic global style, not only the last.
  • Unmounting one of two identical static global styles keeps their CSS in place.
  • v7 only: a global style whose only function sits inside a nested css helper 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.

@changeset-bot

changeset-bot Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a662b14

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

@pullfrog pullfrog Bot 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.

ℹ️ 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: globalStyleComponentId hashes 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. containsFunction walks rules recursively, treating styled components and keyframes values as function-free since they carry their own id in JSON.
  • Wiring — createGlobalStyle.ts now calls the helper instead of generateComponentId(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 in src/test/utils.ts and test-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.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

Comment thread .changeset/global-style-function-id.md Resolved
@pullfrog

pullfrog Bot commented Oct 29, 2026

Copy link
Copy Markdown

Two non-anchored doc-accuracy notes that did not fit an inline anchor:

docs/jest.md enumerates a stale resetStyled() action list. resetStyled() gained a resetGlobalStyleComponentIds() call (src/test/utils.ts:58), but docs/jest.md:57 still spells out an exhaustive list of what it resets (resetGroupIds, resetIdentifiers, mainSheet.names, clearTag(), the class-name mock counter) that now omits it.

An unreleased changeset still promises unconditional SSR/client byte-stability. .changeset/global-style-useid.md:5 states "Rendering output stays byte-stable across SSR, client, and Server Components." The counted ids this PR introduces are assigned by evaluation order, so that holds only when both sides evaluate the colliding function-bodied globals in the same order. Since both changesets ship together, the eventual changelog would assert a guarantee the code no longer makes unconditionally. The new changeset already qualifies hydration correctly; consider qualifying the older claim the same way.

Pullfrog  | View workflow run | via Pullfrog | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot 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.

ℹ️ 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 — globalStyleComponentId now 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 static Infinity/NaN slot 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 — resetStyled no longer calls resetGlobalStyleComponentIds (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.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@quantizor
quantizor force-pushed the cursor/global-style-function-id-4915 branch from 228bb82 to a405fbd Compare November 3, 2026 04:06
@quantizor quantizor changed the title fix: give each function-bodied global style its own id fix(createGlobalStyle): keep each global style's CSS when two differ only in their functions Nov 3, 2026

@pullfrog pullfrog Bot 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.

ℹ️ 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-WebGlobalStyle instanceRules map with registry: Map<componentId, Map<instance, {name, rules}>>, and rewrote rebuildGroup to 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 through removeStyles instead of clearRules.
  • Nested css detection — isStaticRules recurses into nested css-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.ts and its unit suite, dropped the setupTestFramework count reset, and broadened the client/SSR shape matrix with a hydration round-trip test and an isStaticRules table.
  • 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.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot 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.

✅ 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 — InstanceEntry now records the StyleSheet it was computed for, and rebuildGroup skips 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 other mounts 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.

Pullfrog  | Fix it ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@quantizor
quantizor force-pushed the cursor/global-style-function-id-4915 branch from 40c0bf4 to 2579cf5 Compare November 3, 2026 05:18

@pullfrog pullfrog Bot 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.

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 — WebGlobalStyle now stores instance rules in WeakMap<StyleSheet, Map<componentId, Map<instance, { name, rules }>>>; the owning sheet moved out of each entry and into the registry key, and getInstance / deleteInstance / liveInstanceCount take the sheet.
  • Unified group rebuild — rebuildGroup(sheet, instances) clears the group and re-inserts surviving entries in map (mount) order, deduping static entries by entry.name === id; removeStyles rebuilds 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, an AGENTS.md pointer, a broad client/SSR shape matrix, an isStaticRules nested-css table, and an SSR round-trip hydration test.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot 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.

✅ 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. renderStyles now appends only on an instance's first render (styleSheet.server && prev === undefined) and rebuilds on a repeat, which restores the sibling entries a clearRules would 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.

Pullfrog  | Fix it ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@quantizor
quantizor force-pushed the cursor/global-style-function-id-4915 branch from 3265b61 to e7186fa Compare November 3, 2026 23:43

@pullfrog pullfrog Bot 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.

✅ 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 @internal tags — isStatic, the private computeRules, and the private rebuildGroup in WebGlobalStyle carry @internal again, keeping them out of the emitted .d.ts that dist-behavior.test.js scans against the mangle list.
  • Dropped the dead mangle entry — instanceRules left scripts/internalPropertyNames.cjs together 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.

Pullfrog  | Fix it ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot 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.

✅ 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> with createElement/setAttribute, and assigns the escaped CSS via textContent, replacing document.head.innerHTML = sheet.getStyleTags(). Both emissions use a data-styled value other than SC_ATTR_ACTIVE (true vs empty string), so rehydrateSheet treats the reconstructed tag exactly as the parsed one; the existing getStyleTags() 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.

Pullfrog  | Fix it ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@quantizor
quantizor force-pushed the cursor/global-style-function-id-4915 branch from 81c46f9 to a662b14 Compare November 4, 2026 16:36

@pullfrog pullfrog Bot 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.

✅ 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 from 81c46f9.
  • 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 a662b14 with the test's fixed-length method: 14.63kB and 13.45kB, both under their ceilings.

Pullfrog  | Fix it ➔ | View workflow run | Using deepseek-v4.1-flash-1017 (free via Pullfrog for OSS) | 𝕏

@quantizor
quantizor merged commit 8dbc025 into main Nov 4, 2026
8 checks passed
@quantizor
quantizor deleted the cursor/global-style-function-id-4915 branch November 4, 2026 16:46
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.

2 participants