Skip to content

Commit 7e167a4

Browse files
authored
fix: harden 6.7.0 packaging, RSC stylis cache, and client-reference Proxy handling (#5895)
* fix: harden 6.7.0 packaging, RSC stylis cache, and client-reference Proxy handling * fix: harden hasStyledComponentBrand, dev Proxy guard in ComponentStyle, and tarball test timeout * fix(build): stop emitting duplicate native/dist/dist declarations * docs: document web string vs native boolean styledComponentId brand
1 parent 7831e15 commit 7e167a4

18 files changed

Lines changed: 221 additions & 76 deletions

‎.changeset/global-style-function-id.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
'styled-components': patch
33
---
44

5-
Each `createGlobalStyle` applies its own CSS when two global styles differ only in what their functions return, including bodies that are only a function (`createGlobalStyle\`${() => 'html { --a: 1; }'}\``). This is how a CSS reset and a color-mode style are often written together, for example xstyled's Preflight and ColorModeProvider. When both set the same property, the one mounted most recently wins, and a prop or theme update does not change which one that is.
5+
Each `createGlobalStyle` applies its own CSS when two global styles differ only in what their functions return, including bodies that are only a function (`` createGlobalStyle`${() => 'html { --a: 1; }'}` ``). This is how a CSS reset and a color-mode style are often written together, for example xstyled's Preflight and ColorModeProvider. When both set the same property, the one mounted most recently wins, and a prop or theme update does not change which one that is.
66

77
When several mounts of one dynamic global style render on the server, the collected styles include every one of them, not only the last.

‎.github/workflows/test.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,4 @@ jobs:
6868

6969
- run: pnpm install --frozen-lockfile
7070
- run: pnpm test
71+
- run: pnpm check:type-resolution

‎AGENTS.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ mapped below.
2222
branded `Symbol.for()` checks instead of `instanceof`. Verify:
2323
`grep -c 'document\.' native/dist/styled-components.native.cjs.js` must be 0.
2424
See [docs/build-architecture.md](docs/build-architecture.md).
25-
- Server detection needs all three of `__SERVER__`, `IS_RSC` and `styleSheet.server`. Always write
26-
`__SERVER__ || IS_RSC || ssc.styleSheet.server`. The one deliberate exception is documented in
27-
[docs/build-architecture.md](docs/build-architecture.md), which also explains why each flag exists
28-
and when each one lies.
25+
- Server detection draws on three flags (`__SERVER__`, `IS_RSC`, and `ssc.styleSheet.server`), and
26+
they are not interchangeable: each gate picks the smallest subset that covers its cases rather than
27+
combining all three. See [docs/build-architecture.md](docs/build-architecture.md) for why each flag
28+
exists and when each one lies.
2929
- NEVER gate paired cleanup (a `useLayoutEffect` and its teardown) on `__SERVER__` alone. Gate on
3030
`styleSheet.server` or `IS_RSC`.
3131
- NEVER write `typeof React.useRef === 'function'`; that is a runtime conditional hook. Gate on

‎docs/rsc-style-injection.md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,11 @@ before any await.
157157
The override carries exactly four things: `shouldForwardProp`, `styleSheet`, `stylis` and
158158
`stylisPlugins`. A nested manager inherits `stylisPlugins` and `shouldForwardProp` from its parent
159159
when it omits them, which is what lets an inner manager set `namespace` or `enableVendorPrefixes`
160-
while keeping the outer plugins. Passing `stylisPlugins={[]}` explicitly opts out and falls back to
161-
the default stylis instance, configured plugins included, or to a plugin-free instance that keeps a
162-
`namespace` or `enableVendorPrefixes` passed with it. When the resolved stylis instance is the server
163-
default and no `shouldForwardProp` survives resolution, the override is cleared to `null` rather
164-
than set to a redundant object.
160+
while keeping the outer plugins. Passing `stylisPlugins={[]}` opts out of both inherited and
161+
configured plugins and falls back to the plugin-free default stylis instance (`mainStylis`), or to
162+
a plugin-free instance that keeps a `namespace` or `enableVendorPrefixes` passed with it. When the
163+
resolved stylis instance is the server default and no `shouldForwardProp` survives resolution, the
164+
override is cleared to `null` rather than set to a redundant object.
165165

166166
The compiled stylis instance is memoized at module level, because the manager reads and writes it in
167167
one synchronous call. A memoized instance is reused only by a manager with the same plugins array

‎packages/styled-components/package.json‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"scripts": {
1818
"generateErrors": "node scripts/generateErrorMap.js",
1919
"typecheck": "tsc --noEmit",
20-
"prebuild": "rimraf dist && pnpm run generateErrors && pnpm run typecheck",
20+
"prebuild": "rimraf dist native/dist && pnpm run generateErrors && pnpm run typecheck",
2121
"build": "rollup -c",
2222
"postbuild": "pnpm run test:build",
2323
"test:build": "jest -c jest.config.build.js",
@@ -45,7 +45,8 @@
4545
"LICENSE",
4646
"README.md",
4747
"dist",
48-
"native",
48+
"native/dist",
49+
"native/package.json",
4950
"test-utils"
5051
],
5152
"keywords": [

‎packages/styled-components/rollup.config.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,12 @@ const nativeConfig = {
221221
],
222222
plugins: [
223223
nativeTypescriptPlugin,
224-
...commonPlugins,
224+
...basePlugins,
225225
replace({
226226
delimiters: ['', ''],
227227
"typeof React.createContext === 'undefined'": JSON.stringify(false),
228228
}),
229-
minifierPlugin
229+
minifierPlugin,
230230
],
231231
};
232232

‎packages/styled-components/src/base.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
StyleSheetConsumer,
1616
StyleSheetContext,
1717
StyleSheetManager,
18-
configureServerStyles,
1918
} from './models/StyleSheetManager';
2019
/* Import components */
2120
import ThemeProvider, { ThemeConsumer, ThemeContext, useTheme } from './models/ThemeProvider';
@@ -68,7 +67,6 @@ export {
6867
ThemeConsumer,
6968
ThemeContext,
7069
ThemeProvider,
71-
configureServerStyles,
7270
createGlobalStyle,
7371
createTheme,
7472
css,

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,4 +379,19 @@ describe('createGlobalStyle RSC mode', () => {
379379
warn.mockRestore();
380380
}
381381
});
382+
383+
it('keeps each global style CSS when two function-only globals render in one RSC tree', () => {
384+
const First = createGlobalStyle`${() => 'html { --a: 1; }'}`;
385+
const Second = createGlobalStyle`${() => 'html { --b: 2; }'}`;
386+
387+
const html = ReactDOMServer.renderToString(
388+
<>
389+
<First />
390+
<Second />
391+
</>
392+
);
393+
394+
expect(html).toContain('--a:1');
395+
expect(html).toContain('--b:2');
396+
});
382397
});

‎packages/styled-components/src/constructors/test/styled.rsc.test.tsx‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,39 @@ describe('styled RSC mode', () => {
20902090

20912091
expect(seen).toBe(configured);
20922092
});
2093+
2094+
it('keeps throwing on repeated renders of a throwing stylisPlugins array without leaking a prior request stylis', () => {
2095+
const validPlugin: Middleware = element => {
2096+
if (element.type === 'decl') element.return = 'color:green;';
2097+
};
2098+
const badPlugins: Middleware[] = [() => {}];
2099+
const Card = styled.div`
2100+
color: red;
2101+
`;
2102+
2103+
const priorCss = renderRequest(
2104+
<StyleSheetManager namespace="#other-req" stylisPlugins={[validPlugin]}>
2105+
<Card />
2106+
</StyleSheetManager>
2107+
);
2108+
expect(priorCss).toBe('#other-req .CLASS{color:green;}');
2109+
2110+
expect(() =>
2111+
renderRequest(
2112+
<StyleSheetManager stylisPlugins={badPlugins}>
2113+
<Card />
2114+
</StyleSheetManager>
2115+
)
2116+
).toThrow(/stylis plugin/i);
2117+
2118+
expect(() =>
2119+
renderRequest(
2120+
<StyleSheetManager stylisPlugins={badPlugins}>
2121+
<Card />
2122+
</StyleSheetManager>
2123+
)
2124+
).toThrow(/stylis plugin/i);
2125+
});
20932126
});
20942127

20952128
describe('client reference proxy interpolation', () => {
@@ -2149,6 +2182,59 @@ describe('styled RSC mode', () => {
21492182
}).not.toThrow();
21502183
});
21512184

2185+
it('should check isClientReference before the in operator on Proxy client references', () => {
2186+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
2187+
try {
2188+
const throwingHasProxy = new Proxy(
2189+
{},
2190+
{
2191+
get(_target, prop) {
2192+
if (prop === '$$typeof') return Symbol.for('react.client.reference');
2193+
if (prop === '$$id') return 'app/components/throwing-proxy.tsx';
2194+
throw new Error(`Unexpected get(${String(prop)}) on client reference proxy`);
2195+
},
2196+
has(_target, prop) {
2197+
throw new Error(`Unexpected has(${String(prop)}) on client reference proxy`);
2198+
},
2199+
}
2200+
);
2201+
2202+
const alwaysTrueHasProxy = new Proxy(
2203+
{},
2204+
{
2205+
get(_target, prop) {
2206+
if (prop === '$$typeof') return Symbol.for('react.client.reference');
2207+
if (prop === '$$id') return 'app/components/catchall-proxy.tsx';
2208+
return undefined;
2209+
},
2210+
has() {
2211+
return true;
2212+
},
2213+
}
2214+
);
2215+
2216+
const Parent = styled.div`
2217+
${throwingHasProxy as any} {
2218+
font-weight: bold;
2219+
}
2220+
${() => throwingHasProxy as any} {
2221+
margin: 0;
2222+
}
2223+
${alwaysTrueHasProxy as any} {
2224+
color: red;
2225+
}
2226+
padding: 8px;
2227+
`;
2228+
2229+
const css = extractStyleContents(ReactDOMServer.renderToString(<Parent />));
2230+
expect(css).toContain('padding:8px');
2231+
expect(css).not.toContain('.undefined');
2232+
expect(warnSpy).toHaveBeenCalledTimes(3);
2233+
} finally {
2234+
warnSpy.mockRestore();
2235+
}
2236+
});
2237+
21522238
it('should skip client reference interpolation and render remaining CSS', () => {
21532239
const ClientButton = createNamedExportProxy('ClientButton');
21542240

‎packages/styled-components/src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import styled, { LibraryStyled, Styled, StyledInstance } from './constructors/styled';
22

33
export * from './base';
4+
export { configureServerStyles } from './models/StyleSheetManager';
45
export { default as stylisPluginRSC } from './utils/stylisPluginRSC';
56
export {
67
CSSKeyframes,

0 commit comments

Comments
 (0)