Skip to content

Commit db4f940

Browse files
authored
perf: fix createGlobalStyle test performance regression (#5679)
Two changes to eliminate unnecessary overhead in client/test environments: 1. Move synchronous renderStyles to only run on server/RSC paths. In client mode, the useLayoutEffect already handles style rendering, so the synchronous call during component execution was double work. 2. Remove removeGlobalStyleTag from client cleanup. This function does querySelectorAll for data-styled-global tags, which only exist after SSR hydration. In pure client/test environments it was a no-op DOM query on every global style unmount. globalStyle.removeStyles already handles client-side StyleSheet cleanup. Fixes #5674
1 parent 6b7fe3f commit db4f940

2 files changed

Lines changed: 12 additions & 10 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+
Fix test performance regression in 6.3.x by eliminating double style rendering in `createGlobalStyle` and removing unnecessary DOM queries during cleanup in client/test environments.

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import GlobalStyle from '../models/GlobalStyle';
44
import { useStyleSheetContext } from '../models/StyleSheetManager';
55
import { DefaultTheme, ThemeContext } from '../models/ThemeProvider';
66
import StyleSheet from '../sheet';
7-
import { removeGlobalStyleTag } from '../sheet/dom';
87
import { getGroupForId } from '../sheet/GroupIDAllocator';
98
import { ExecutionContext, ExecutionProps, Interpolation, Stringifier, Styles } from '../types';
109
import { checkDynamicCreation } from '../utils/checkDynamicCreation';
@@ -59,27 +58,25 @@ export default function createGlobalStyle<Props extends object>(
5958
);
6059
}
6160

62-
// Render styles during component execution
63-
const shouldRenderStyles = typeof window === 'undefined' || !ssc.styleSheet.server;
64-
if (shouldRenderStyles) {
65-
renderStyles(instance, props, ssc.styleSheet, theme, ssc.stylis);
61+
// Render styles during component execution for server/RSC (where effects don't run).
62+
// In client mode, styles are rendered in the useLayoutEffect below to avoid double work.
63+
if (__SERVER__ || IS_RSC) {
64+
const shouldRenderStyles = typeof window === 'undefined' || !ssc.styleSheet.server;
65+
if (shouldRenderStyles) {
66+
renderStyles(instance, props, ssc.styleSheet, theme, ssc.stylis);
67+
}
6668
}
6769

6870
// Client-side lifecycle: render styles in effect and clean up on unmount.
6971
// __SERVER__ and IS_RSC are build/module-level constants, so this doesn't violate rules of hooks.
7072
if (!__SERVER__ && !IS_RSC) {
7173
React.useLayoutEffect(() => {
72-
// Re-render styles on every effect run to self-heal after any cleanup
73-
// (e.g. StrictMode's simulated unmount/remount, error recovery).
74-
// useLayoutEffect runs synchronously before paint, so the brief
75-
// remove→re-add in cleanup→mount is never visible to the user.
7674
if (!ssc.styleSheet.server) {
7775
renderStyles(instance, props, ssc.styleSheet, theme, ssc.stylis);
7876
}
7977

8078
return () => {
8179
globalStyle.removeStyles(instance, ssc.styleSheet);
82-
removeGlobalStyleTag(styledComponentId, ssc.styleSheet.options.target);
8380
};
8481
}, [instance, props, ssc.styleSheet, theme, ssc.stylis]);
8582
}

0 commit comments

Comments
 (0)