Skip to content

in development, updating a createGlobalStyle doesn't remove the old version #5641

Closed

Description

@laurenskling

Running on Next 16.1.1, SC 6.3.5
when I update the styles in a createGlobalStyle on a server-only file,
a new entry is added to the head:

<style data-styled-global="sc-global-blobyl" data-href="sc-global-sc-global-blobyl-1-glDrNT" data-precedence="styled-components">
body{hyphens:none;}/*!sc*/
h1,h2,h3,h4,h5,h6{margin:0px 0px 1rem;}/*!sc*/
</style>

But the previous version isn't removed. Thus developing feels buggy as old styles might win specificity etc.

Image

Activity

  1. quantizor commented on Jan 13, 2026

    @quantizor
    Contributor

    Related to #5632

  2. quantizor commented on Jan 13, 2026

    @quantizor
    Contributor

    Hi, please try out styled-components@prerelease (6.3.7-prerelease.0) and let me know if it helps.

  3. laurenskling commented on Jan 13, 2026

    @laurenskling
    Author

    That release gets me stuck on:

    Image

    the full file it's (now) crashing on:

    'use client';
    
    import { ThemeProvider } from 'styled-components';
    
    const ClientThemeProvider = ({
      children,
      theme,
    }) => (
      <ThemeProvider theme={theme}>
        {children}
      </ThemeProvider>
    );
    
    export default ClientThemeProvider;
    
  4. laurenskling commented on Jan 13, 2026

    @laurenskling
    Author

    If I remove the ThemeProvider the error is still, useRef only works in Client Components. Add the "use client" directive at the top of the file to use it. Read more: https://nextjs.org/docs/messages/react-client-hook-in-server-component

  5. quantizor commented on Jan 13, 2026

    @quantizor
    Contributor

    That's to be expected, ThemeProvider uses React context which is not available under RSC

  6. quantizor commented on Jan 13, 2026

    @quantizor
    Contributor

    I'll check into the useRef usage, I wasn't aware that particular hook is not available in RSC.

  7. laurenskling commented on Jan 13, 2026

    @laurenskling
    Author

    That's to be expected, ThemeProvider uses React context which is not available under RSC

    It's a No-op in 6.3.x RSC remember ;) But it was a false flag. I debugged it down to the <GlobalStyle /> in my code

  8. quantizor commented on Jan 13, 2026

    @quantizor
    Contributor
  9. laurenskling commented on Jan 13, 2026

    @laurenskling
    Author

    sadly still the case

  10. quantizor commented on Jan 13, 2026

    @quantizor
    Contributor

    How? Are you sure the new version is installed? The hook usage is physically guarded now inside the !IS_RSC check...

  11. laurenskling commented on Jan 13, 2026

    @laurenskling
    Author

    I'm sorry, I ment the original bug report, not the useRef thingy

  12. laurenskling commented on Jan 13, 2026

    @laurenskling
    Author
  13. quantizor commented on Jan 13, 2026

    @quantizor
    Contributor

    Any chance you could share the code snippets for those and the SSR styled-components registry snippet?

    [email protected] has a candidate fix for the original dev issue you mentioned

  14. laurenskling commented on Jan 14, 2026

    @laurenskling
    Author
  15. laurenskling commented on Jan 14, 2026

    @laurenskling
    Author
    Schermopname.2026-01-14.om.09.46.16.mov

    The SSR starts with a big data-href server version of the globalstyle.
    When I add background-color: green; to my body, you see a second style tag. The I change to blue, third style tag. Then I remove that line, make it go back to the background-color: white;, a fourth shows up (note that this is the same css as the SSRed)

    I change to width: 25%, fifth style tag. I remove it again. No new style tag. Here it sees that this version of the styles are already rendered in the fourth style tag. This is where the bug shines. I removed the width, but it's still in my browser. next: width: 40%, sixth style tag. Remove that again. Again, it sees that we already have this version of the styles, so no new tag, but we remain at 40%.

    Image
  16. laurenskling commented on Jan 14, 2026

    @laurenskling
    Author

    I have a server-only createGlobalStyle, which I conditionally render in a use client. These styles are added when the render happens, but not removed when the render removes it. I've already wrote about this. It's basically the same bug I guess.

    When I put this createGlobalStyle in a use client, it works as expected. The style line goes in and out of
    <style data-styled="active" data-styled-version="6.3.7-prerelease.2">

  17. quantizor commented on Jan 14, 2026

    @quantizor
    Contributor

    Could you try out [email protected] when you get a moment?

  18. laurenskling commented on Jan 15, 2026

    @laurenskling
    Author

    They are still never removed. I think I know why:
    https://github.com/styled-components/styled-components/pull/5638/files#diff-b8702cbcaf39eaa95deb156bf4132fef9fd7096c4f87679cf656105070c38bc8R88
    this is the line that does the removing right? It is inside the !IS_RSC check of GlobalStyleComponent. You would think: that will be true on the client, so it works. But I don't see this code being executed on the client. I'm not an expert on RSC, But I believe that there is no client mounting going on when it's not marked as use client, so this code is not hydrated, it is not run. This useLayoutEffect hook is never added to the Node.

  19. laurenskling commented on Jan 19, 2026

    @laurenskling
    Author

    This issue persists in 6.3.8

  20. quantizor commented on Feb 7, 2026

    @quantizor
    Contributor

    Fixing in the next version

  21. laurenskling commented on Feb 8, 2026

    @laurenskling
    Author

    In 6.3.9 global styles are unmounted and re-added with a new data-styled-global tag, thus working. Thank you.

    Just a note: global styles are now rendered in the body, not in the head. This is as intended?

  22. quantizor commented on Feb 8, 2026

    @quantizor
    Contributor

    In 6.3.9 global styles are unmounted and re-added with a new data-styled-global tag, thus working. Thank you.

    Just a note: global styles are now rendered in the body, not in the head. This is as intended?

    Ah, thanks for reporting. No should probably be in the head. Will look later, or if you have time would welcome a PR!

  23. laurenskling commented on Feb 8, 2026

    @laurenskling
    Author

    As you made them in "regular" react component (or something like this I read somewhere in your comments). They seem to be rendered in the tree at the spot you put the tag. Which makes sense. But I guess not what you'd want.

    Is it the precedence that made it go to the head before?

  24. quantizor commented on Mar 19, 2026

    @quantizor
    Contributor

    The original bug (global styles not being removed during HMR) was fixed in 6.3.9.

    Regarding the remaining note about server-only createGlobalStyle rendering <style> tags in the body instead of <head>: this is a known limitation of RSC mode. Without React 19's precedence attribute (which we intentionally avoid for global styles due to unmount persistence issues), there's no mechanism for React to hoist the tag to <head> during streaming.

    Workaround: If you need global styles in <head>, use the 'use client' + StyledComponentsRegistry pattern.

    No other runtime CSS-in-JS library has solved this — Emotion doesn't support RSC at all, and zero-runtime libraries (Pigment CSS, Panda, vanilla-extract) sidestep it with build-time extraction. We'll revisit when React provides better primitives for style injection during streaming.

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