Repository navigation
fix: resolve createGlobalStyle SSR issue in v6.3.x - #5638
Conversation
🦋 Changeset detectedLatest commit: d398d63 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 |
|
@copilot See this comment specifically, we're missing something #5632 (comment) |
Found the issue. The Fixed in commit Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
Investigated both issues and the React StrictMode behavior. Issue #5641 (HMR styles not removed): This is a separate issue related to React 19's style hoisting with the React StrictMode fix (commit
Added comprehensive tests for StrictMode double-render behavior. All 406 tests pass. |
Static global styles are now only injected once and skipped on re-renders, preventing issues with React 18/19 concurrent rendering where the brief removal and re-addition of styles could cause visual glitches or interfere with React 19's style hoisting behavior. Co-authored-by: quantizor <[email protected]>
- Static styles now only inject once (skip if already exists) - Use queueMicrotask for StrictMode-aware cleanup coordination - Use React.useRef for per-instance cleanup state tracking - Updated tests to handle async cleanup with await Promise.resolve() Co-authored-by: quantizor <[email protected]>
Changed from `useRef<{ shouldRemove: boolean }>` to `useRef<boolean>` since
the ref object itself is stable and there's no need for the extra wrapper.
Co-authored-by: quantizor <[email protected]>
7df0a47 to
1540944
Compare
When a server-defined createGlobalStyle is conditionally rendered in a client component, React 19's hoisted style tags with precedence attribute would persist in the DOM even after the component unmounts. This caused accumulated style tags and incorrect CSS. The fix adds removeGlobalStyleTag() to sheet/dom.ts which removes all SSR-rendered inline style tags (data-styled-global) when the GlobalStyle cleanup effect runs on the client.
8a6d1d1 to
1a33199
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 3 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
This is the final PR Bugbot will review for you during this billing cycle
Your free Bugbot reviews will reset on February 7
Details
You are on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle.
To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.
| // These persist in the DOM because React 19's hoisted styles don't unmount | ||
| removeGlobalStyleTag(styledComponentId); | ||
| } | ||
| }); |
There was a problem hiding this comment.
Race condition: stale ref allows microtask to remove active styles
High Severity
The shouldRemoveRef is created per React component instance, but the instance number is shared via the instanceMap WeakMap across all React instances of the same GlobalStyle. When a component unmounts and a new instance immediately remounts (before the microtask runs), the new instance creates a fresh ref (refB) while the scheduled microtask still holds a closure over the old ref (refA). Since refA.current remains true, the microtask proceeds to remove styles that the newly-mounted instance needs. For static styles, the check hasNameForId(id, id) returns true during the new render so styles aren't re-injected, leaving the component without styles.
| // These persist in the DOM because React 19's hoisted styles don't unmount | ||
| removeGlobalStyleTag(styledComponentId); | ||
| } | ||
| }); |
There was a problem hiding this comment.
Concurrent GlobalStyle instances: one unmount removes styles for all
Medium Severity
When multiple React component instances of the same GlobalStyle are mounted simultaneously (e.g., <><GlobalStyle /><GlobalStyle /></>), unmounting one instance removes styles for all. Each instance creates its own shouldRemoveRef, but they share the same instance number from the instanceMap WeakMap. When Instance A unmounts, its cleanup checks refA.current (which is true) and removes styles for the shared instance. Instance B's ref being false doesn't prevent this since the microtask captured Instance A's ref, not Instance B's.
| // These persist in the DOM because React 19's hoisted styles don't unmount | ||
| removeGlobalStyleTag(styledComponentId); | ||
| } | ||
| }); |
There was a problem hiding this comment.
Missing style cleanup when StyleSheet dependencies change causes leak
Low Severity
When the effect's dependencies (instance or ssc.styleSheet) change, the old styles are never cleaned up. The cleanup function schedules a microtask with shouldRemoveRef.current = true, but the new effect immediately runs and sets shouldRemoveRef.current = false before the microtask executes. The microtask then skips cleanup because the ref is false. The previous synchronous cleanup directly called globalStyle.removeStyles() and properly removed old styles. This causes styles to leak in the old StyleSheet when StyleSheetManager's target or sheet prop changes dynamically.
Fix createGlobalStyle compatibility with React StrictMode
createGlobalStylein React StrictModequeueMicrotaskfor StrictMode-aware cleanupReact.useReffor per-instance state trackingProblem
In React StrictMode, components are unmounted and remounted to help identify side effects. The previous implementation had issues:
Solution
queueMicrotaskto delay cleanup. In StrictMode's remount, the next effect runs before the microtask, canceling the cleanup. In real unmount, no new effect runs, so styles are removed.React.useRef<boolean>to track cleanup state per component instanceRelated Issues
createGlobalStylein v6.3.x #5632 (createGlobalStyle SSR issue)Original prompt
createGlobalStylein v6.3.x #5632✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
Note
Ensures
createGlobalStylebehaves correctly in StrictMode, concurrent rendering, and RSC, while cleaning up SSR hoisted tags and tightening RSC guards.useRef+queueMicrotaskstyle[data-styled-global]tags via newremoveGlobalStyleTag!IS_RSC ? React.useContext(...)usage across core (createGlobalStyle, withTheme, StyledComponent, ThemeProvider, StyleSheetManager); emit stablehreffor RSC style tags (supports HMR) with new RSC tests@types/reactand@types/react-domto peer depsstyled.brin supported DOM elementsWritten by Cursor Bugbot for commit d398d63. This will update automatically on new commits. Configure here.