Repository navigation
fix: prevent RSC style tag accumulation across HMR cycles - #5702
Conversation
🦋 Changeset detectedLatest commit: 818fd1a 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 RSC hot-reload issue where inline <style> tags (and their CSS) can accumulate across HMR cycles due to a persistent module-level mainSheet, and adjusts RSC keyframe emission to avoid dedup instability when keyframes register mid-render.
Changes:
- Reset the RSC module-level
mainSheetonce per server render usingReact.cacheto prevent cross-HMR style accumulation. - Emit keyframe rules into a dedicated inline
<style>tag and dedupe keyframes separately from component CSS. - Add a dev-only sandbox UI badge to monitor
<style>tag counts during HMR.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/styled-components/src/models/StyleSheetManager.tsx | Adds per-render mainSheet reset hook for RSC via React.cache. |
| packages/styled-components/src/models/StyledComponent.ts | Changes RSC CSS emission to separate keyframes into their own <style> tag with new dedup behavior. |
| packages/styled-components/package.json | Updates package version to a prerelease. |
| packages/sandbox/app/layout.tsx | Adds the dev-only StyleTagMonitor to the sandbox layout. |
| packages/sandbox/app/components/style-tag-monitor.tsx | Introduces a MutationObserver-based style tag counter overlay for dev monitoring. |
| .changeset/fix-rsc-hmr-accumulation.md | Adds a changeset entry describing the fix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Reset mainSheet once per server render via React.cache to clear stale CSS from previous HMR cycles. Keyframe rules are emitted in a dedicated <style> tag with separate dedup to avoid inconsistent CSS strings when keyframes register mid-render. Closes #5670
a6ce715 to
818fd1a
Compare
Summary
mainSheet(names, keyframeIds, tag) once per server render viaReact.cacheso stale CSS from previous HMR cycles doesn't persist in the module-level singleton<style>tag with separate dedup to avoid inconsistent CSS strings when keyframes register mid-renderDetails
Root cause: In RSC mode,
mainSheetis a module-level singleton that persists across HMR cycles because webpack/Turbopack don't re-evaluate thestyled-componentsmodule on file changes.StyleSheetManageris a no-op in RSC, so there's no cleanup path—CSS from stale renders accumulates indefinitely.Fix: Use
React.cache(React 19+, scoped per server render) to run a one-time reset ofmainSheet.names,mainSheet.keyframeIds, andmainSheet.tagat the start of each render. This preserves within-render dedup (hasNameForIdfor shared base CSS) while preventing cross-HMR accumulation. All three are safe to clear because components re-inject their CSS and re-register keyframes viakeyframe.inject()during render.Keyframe separation: Previously, all keyframe CSS was prepended to every component's inline
<style>tag. Since keyframes register mid-render (when the first keyframe component renders), early components got no keyframe prefix while late components did—breaking thegetEmittedCSSstring-based dedup. Now keyframes are emitted in their own<style>tag, deduped separately by keyframe ID.Performance: Benchmarked with 1000 iterations of
renderToStringon a 12-component RSC tree (inheritance chains, keyframes, dynamic props). No measurable regression: ~0.077ms mean / 0.074ms p50 for both baseline and fix.Closes #5670