Skip to content

fix: prevent HMR style tag accumulation in RSC mode - #5684

Closed
quantizor wants to merge 2 commits into
mainfrom
fix/hmr-style-tag-accumulation
Closed

quantizor wants to merge 2 commits into
mainfrom
fix/hmr-style-tag-accumulation

Conversation

@quantizor

Copy link
Copy Markdown
Contributor

Summary

  • In development, use stable hrefs (sc-{componentId} only) on RSC <style> tags so React 19 replaces them on hot reload instead of treating each CSS content change as a new permanent resource
  • In production, content-aware hrefs (including name hashes) are preserved for correct dynamic variant deduplication
  • Adds 3 new RSC tests validating HMR-compatible href behavior

Background

When styles change during HMR, the CSS content hash changes, producing a new href. React 19 treats <style precedence="..."> tags as permanent resources keyed by href — two different hrefs = two separate tags, neither removed. This causes stale style tags to accumulate, and stale &-based selectors keep matching because they use the stable component ID class.

Tradeoff

Dynamic prop variants (e.g., $bg="red" vs $bg="blue") may incorrectly dedup in dev mode since they share the same stable href. This is acceptable because:

  • It's dev-only — production uses content-aware hrefs
  • Both variants' CSS still renders (last one wins)
  • HMR correctness is more important than variant isolation in dev

History check

Test plan

  • All 506 tests pass (503 existing + 3 new HMR tests)
  • Build passes with bundlewatch
  • All 17 RSC tests pass
  • Manual HMR test with Next.js sandbox app

Closes #5670

In development, use stable hrefs (componentId only) on RSC style tags
so React 19 replaces them on hot reload instead of treating each CSS
content change as a new permanent resource. In production, content-aware
hrefs are preserved for correct dynamic variant deduplication.

The tradeoff: dynamic prop variants may incorrectly dedup in dev mode
(e.g., two instances with different $bg props share one style tag).
This is acceptable since dev mode prioritizes HMR correctness and the
styles still render — just with one variant potentially overriding
the other until a full page refresh.

Closes #5670
@changeset-bot

changeset-bot Bot commented Mar 19, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 67016f7

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

Instead of using stable hrefs (which causes React 19 to deduplicate
dynamic variant style tags), omit precedence and href entirely in
development. React then treats <style> as a regular element that
unmounts on re-render, preventing stale tag accumulation during HMR.

Production behavior is unchanged — content-aware hrefs are preserved
for correct deduplication of shared base styles.
@quantizor

Copy link
Copy Markdown
Contributor Author

Available to test via [email protected]

@quantizor quantizor closed this Mar 21, 2026
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

1 participant