Skip to content

fix: prevent RSC style tag accumulation across HMR cycles - #5702

Merged
quantizor merged 3 commits into
mainfrom
fix/rsc-hmr-style-accumulation
Mar 24, 2026
Merged

quantizor merged 3 commits into
mainfrom
fix/rsc-hmr-style-accumulation

Conversation

@quantizor

@quantizor quantizor commented Mar 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Reset mainSheet (names, keyframeIds, tag) once per server render via React.cache so stale CSS from previous HMR cycles doesn't persist in the module-level singleton
  • Emit keyframe rules in a dedicated <style> tag with separate dedup to avoid inconsistent CSS strings when keyframes register mid-render

Details

Root cause: In RSC mode, mainSheet is a module-level singleton that persists across HMR cycles because webpack/Turbopack don't re-evaluate the styled-components module on file changes. StyleSheetManager is 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 of mainSheet.names, mainSheet.keyframeIds, and mainSheet.tag at the start of each render. This preserves within-render dedup (hasNameForId for shared base CSS) while preventing cross-HMR accumulation. All three are safe to clear because components re-inject their CSS and re-register keyframes via keyframe.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 the getEmittedCSS string-based dedup. Now keyframes are emitted in their own <style> tag, deduped separately by keyframe ID.

Performance: Benchmarked with 1000 iterations of renderToString on 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

@changeset-bot

changeset-bot Bot commented Mar 23, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 818fd1a

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

Copilot AI 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.

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 mainSheet once per server render using React.cache to 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.

Copilot AI 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.

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.

Comment thread packages/styled-components/package.json Resolved
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
@quantizor
quantizor force-pushed the fix/rsc-hmr-style-accumulation branch from a6ce715 to 818fd1a Compare March 24, 2026 00:07
@quantizor
quantizor merged commit 718a78e into main Mar 24, 2026
5 checks passed
@quantizor
quantizor deleted the fix/rsc-hmr-style-accumulation branch March 24, 2026 00:08
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.

hotloading styles doesn't unmount the previous style tag

2 participants