Skip to content

Commit 8e8c282

Browse files
Copilotquantizor
andcommitted
fix: use __SERVER__ build constant instead of useLayoutEffect check in createGlobalStyle
Co-authored-by: quantizor <[email protected]>
1 parent b0831e8 commit 8e8c282

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"styled-components": patch
3+
---
4+
5+
Fixed `createGlobalStyle` to not use `useLayoutEffect` on the server, which was causing a warning and broken styles in v6.3.x. The check `typeof React.useLayoutEffect === 'function'` is not reliable for detecting server vs client environments in React 18+, so we now use the `__SERVER__` build constant instead.

‎packages/styled-components/src/constructors/createGlobalStyle.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import generateComponentId from '../utils/generateComponentId';
1313
import { hash } from '../utils/hash';
1414
import css from './css';
1515

16+
declare const __SERVER__: boolean;
17+
1618
export default function createGlobalStyle<Props extends object>(
1719
strings: Styles<Props>,
1820
...interpolations: Array<Interpolation<Props>>
@@ -66,8 +68,8 @@ export default function createGlobalStyle<Props extends object>(
6668
}
6769

6870
// Client-side cleanup: conditionally use useLayoutEffect
69-
// The IS_RSC check is module-level and deterministic, so this doesn't violate rules of hooks
70-
if (!IS_RSC && typeof React.useLayoutEffect === 'function') {
71+
// The __SERVER__ and IS_RSC checks are module-level and deterministic, so this doesn't violate rules of hooks
72+
if (!__SERVER__ && !IS_RSC) {
7173
React.useLayoutEffect(() => {
7274
return () => {
7375
globalStyle.removeStyles(instance, ssc.styleSheet);

‎packages/styled-components/src/constructors/test/createGlobalStyle.ssr.test.tsx‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ describe(`createGlobalStyle`, () => {
3333
`"[data-test-inject]{ color:red; } data-styled.g1[id="sc-global-a1"]{ content:"sc-global-a1,"} "`
3434
);
3535
});
36+
37+
it(`should not emit useLayoutEffect warning during SSR`, () => {
38+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});
39+
40+
const sheet = new ServerStyleSheet();
41+
const Component = createGlobalStyle`body { background: red; }`;
42+
context.renderToString(sheet.collectStyles(<Component />));
43+
44+
// Check that useLayoutEffect warning was not emitted
45+
const useLayoutEffectWarning = consoleErrorSpy.mock.calls.find(call =>
46+
call.some(arg => typeof arg === 'string' && arg.includes('useLayoutEffect'))
47+
);
48+
expect(useLayoutEffectWarning).toBeUndefined();
49+
50+
consoleErrorSpy.mockRestore();
51+
});
3652
});
3753

3854
function stripOuterHTML(html: string) {

0 commit comments

Comments
 (0)