Skip to content

createGlobalStyle: global styles whose rules are only functions share one id, so only the last one renders (6.4.0+) #5845

Done

Description

@quantizor

Two createGlobalStyle components whose whole body is a function interpolation end up with the same component id. Since 6.4.0, only the last one's CSS renders. This happens in SSR and in the browser.

const A = createGlobalStyle`${() => 'body{--a:1}'}`
const B = createGlobalStyle`${() => 'html{--b:1}'}`
// render <A /> and <B />: only --b is in the output

// same with the function-argument form
const C = createGlobalStyle(() => css`body{--a:1}`)
const D = createGlobalStyle(() => css`html{--b:1}`)

Real-world impact: xstyled's <Preflight /> and <ColorModeProvider /> both use this shape, so rendering Preflight drops the color mode variables and breaks dark mode.

Which shapes collide

A and B 6.3.12 6.4.0 to 6.6.0, v7 prerelease, main
both whole-function bodies A and B B only
both createGlobalStyle(fn) A and B B only
function inside a rule (body{--a:${() => 1}}) A and B A and B
one static, one whole-function A and B A and B

Cause

createGlobalStyle derives its id from generateComponentId(JSON.stringify(rules)). JSON.stringify serializes functions as null, so every global style whose rules are only functions hashes to the same sc-global-* id. That hashing is old. What changed in 6.4.0 is that instances of one id now share a single style group, so the later component's rules replace the earlier one's.

Repro

const React = require('react')
const { renderToString } = require('react-dom/server')
const { createGlobalStyle, ServerStyleSheet } = require('styled-components')

const A = createGlobalStyle`${() => 'body{--a:1}'}`
const B = createGlobalStyle`${() => 'html{--b:1}'}`
const sheet = new ServerStyleSheet()
renderToString(sheet.collectStyles(React.createElement(React.Fragment, null, React.createElement(A), React.createElement(B))))
const css = sheet.getStyleTags()
console.log(css.includes('--a'), css.includes('--b')) // 6.3.12: true true; 6.4.0+: false true

Activity

  1. pullfrog commented on Oct 29, 2026

    @pullfrog
  2. quantizor commented on Nov 25, 2026

    @quantizor
    ContributorAuthor

    Fixed in 6.7

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