Skip to content

hotloading styles doesn't unmount the previous style tag #5670

Closed

Description

@laurenskling

Tested on 6.3.11, server components:

When I update my styles, a new style tag is added with the new styles. Normally, this goes well because there's a new className. But:

export const CategoryBadge = styled.div`
  font-size: normal;
  font-weight: bold;

  & + & {
    margin-left: 4.5rem;
  }
`;
<style data-href="sc-styles__CategoryBadge-sc-4ae1dfd-0-dEiWqV" data-precedence="styled-components">.dEiWqV{font-size:normal;font-weight:bold;}/*!sc*/
.styles__CategoryBadge-sc-4ae1dfd-0+.styles__CategoryBadge-sc-4ae1dfd-0{margin-left:1.5rem;}/*!sc*/
</style>
<style data-href="sc-styles__CategoryBadge-sc-4ae1dfd-0-fxbJLw" data-precedence="styled-components">.fxbJLw{font-size:normal;font-weight:bold;}/*!sc*/
.styles__CategoryBadge-sc-4ae1dfd-0+.styles__CategoryBadge-sc-4ae1dfd-0{margin-left:4.5rem;}/*!sc*/
</style>

Because I use the & symbol, the permanent className is used. So these style tags all keep applying styles.

Image

Activity

  1. quantizor commented on Mar 19, 2026

    @quantizor
    Contributor

    Confirmed — in RSC mode, each HMR style change produces a new content-hash-based href, and React 19 treats each <style precedence="..."> as a separate permanent resource that is never removed. Stale &-based selectors keep matching because they use the stable component ID class.

    The fix is to skip precedence on RSC style tags in development mode so React doesn't hoist them as permanent resources. This only affects dev — production styles are immutable so persistence is correct there.

  2. added a commit that references this issue on Mar 19, 2026
    ceafa93
  3. quantizor commented on Mar 19, 2026

    @quantizor
    Contributor

    Revised the approach in #5684. Instead of using stable hrefs in dev mode (which causes React 19 to incorrectly deduplicate style tags for dynamic prop variants), the updated fix omits precedence and href entirely in development.

    Without these props, React treats <style> as a regular element — not a resource. Regular elements unmount naturally on re-render, so stale style tags from HMR cycles are cleaned up automatically. This avoids the original accumulation issue without introducing dedup bugs for components with dynamic props.

    Production behavior is unchanged: content-aware precedence/href are preserved for correct deduplication.

    You can test with [email protected].

  4. quantizor commented on Mar 21, 2026

    @quantizor
    Contributor

    [email protected] is published with the createGlobalStyle shared-group rearchitecture and RSC inline style tag changes (no more precedence/href attributes).

    These changes should significantly reduce HMR style accumulation since RSC style tags are now plain inline <style> elements that React doesn't treat as permanent Float resources.

    See Version Packages PR #5687 for the full changelog.

    Please test with npm i [email protected] and report back.

  5. quantizor commented on Mar 24, 2026

    @quantizor
    Contributor

    Fixed in [email protected] (PR #5702). The module-level stylesheet singleton now resets once per server render via React.cache, and keyframe rules are emitted in a dedicated <style> tag with separate deduplication. Install with npm i styled-components@test to verify.

  6. added a commit that references this issue on Mar 24, 2026
    718a78e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions