Skip to content

fix: use __SERVER__ build constant instead of useLayoutEffect check in createGlobalStyle - #5633

Merged
quantizor merged 2 commits into
mainfrom
copilot/fix-createglobalstyle-behavior
Jan 11, 2026
Merged

quantizor merged 2 commits into
mainfrom
copilot/fix-createglobalstyle-behavior

Conversation

Copilot AI commented Jan 11, 2026 •

Copy link
Copy Markdown
Contributor

createGlobalStyle in v6.3.x uses typeof React.useLayoutEffect === 'function' to detect client-side rendering, but this check is unreliable in React 18+ where useLayoutEffect exists on the server (it just warns when called). This causes the SSR warning and styles being incorrectly removed during hydration.

Changes

  • createGlobalStyle.ts: Replace runtime useLayoutEffect existence check with __SERVER__ build constant, which Rollup replaces at build time (true for server builds, false for browser builds)
  • SSR test: Add test verifying no useLayoutEffect warning is emitted during server rendering

Result

  • Server build: useLayoutEffect block is tree-shaken entirely (0 occurrences)
  • Browser build: useLayoutEffect retained for cleanup (1 occurrence)
- if (!IS_RSC && typeof React.useLayoutEffect === 'function') {
+ if (!__SERVER__ && !IS_RSC) {
    React.useLayoutEffect(() => {

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • firebasedynamiclinks.googleapis.com
    • Triggering command: /usr/local/bin/node node /home/REDACTED/work/styled-components/styled-components/node_modules/.bin/bundlewatch (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Changed behavior for createGlobalStyle in v6.3.x</issue_title>
<issue_description>Updating to v6.3.0 did break my global styles. I also see the following warning:

Warning: useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format. This will lead to a mismatch between the initial, non-hydrated UI and the intended UI. To avoid this, useLayoutEffect should only be used in components that render exclusively on the client. See https://reactjs.org/link/uselayouteffect-ssr for common fixes.
    at u (/Users/koenpunt/Developer/koenpunt/kakhiel-web-secondary/node_modules/styled-components/src/constructors/createGlobalStyle.ts:32:17)

I think the culprit is here, because I do see a flash of the expected styles, but then those styles are removed.

if (!IS_RSC && typeof React.useLayoutEffect === 'function') {
React.useLayoutEffect(() => {
return () => {
globalStyle.removeStyles(instance, ssc.styleSheet);
};
}, [instance, ssc.styleSheet]);
}

Before the condition was if (!__SERVER__) {, and I think that using the presence of React.useLayoutEffect isn't a reliable check for whether the code is running in a client environment.

I'm using React 18.3.1.</issue_description>

<agent_instructions>Don't forget a changeset</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@changeset-bot

changeset-bot Bot commented Jan 11, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a211361

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 changed the title [WIP] Fix changed behavior for createGlobalStyle in v6.3.x fix: use __SERVER__ build constant instead of useLayoutEffect check in createGlobalStyle Jan 11, 2026
Copilot AI requested a review from quantizor January 11, 2026 01:21
@quantizor
quantizor marked this pull request as ready for review January 11, 2026 01:51
@quantizor
quantizor merged commit 8e8c282 into main Jan 11, 2026
@quantizor
quantizor deleted the copilot/fix-createglobalstyle-behavior branch January 11, 2026 01:53
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.

Changed behavior for createGlobalStyle in v6.3.x

2 participants