Skip to content

Commit bdf2c99

Browse files
gnoffAndyPengc12
authored andcommitted
[Float] Nonce preload support (facebook#26939)
Some browsers, with some CSP configuration, will not preload a script if the prelaod link tag does not provide a valid nonce attribute. This change adds the ability to specify a nonce for `ReactDOM.preload(..., { as: "script" })`
1 parent 2e04731 commit bdf2c99

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,6 +2233,7 @@ function preloadPropsFromPreloadOptions(
22332233
crossOrigin: as === 'font' ? '' : options.crossOrigin,
22342234
integrity: options.integrity,
22352235
type: options.type,
2236+
nonce: options.nonce,
22362237
fetchPriority: options.fetchPriority,
22372238
};
22382239
}

packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5532,6 +5532,7 @@ function preloadPropsFromPreloadOptions(
55325532
crossOrigin: as === 'font' ? '' : options.crossOrigin,
55335533
integrity: options.integrity,
55345534
type: options.type,
5535+
nonce: options.nonce,
55355536
fetchPriority: options.fetchPriority,
55365537
};
55375538
}

packages/react-dom/src/__tests__/ReactDOMFloat-test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3981,6 +3981,45 @@ body {
39813981
</html>,
39823982
);
39833983
});
3984+
3985+
it('supports nonce', async () => {
3986+
function App({url}) {
3987+
ReactDOM.preload(url, {as: 'script', nonce: 'abc'});
3988+
return 'hello';
3989+
}
3990+
3991+
await act(() => {
3992+
renderToPipeableStream(<App url="server" />).pipe(writable);
3993+
});
3994+
3995+
expect(getMeaningfulChildren(document)).toEqual(
3996+
<html>
3997+
<head />
3998+
<body>
3999+
<div id="container">
4000+
<link rel="preload" as="script" href="server" nonce="abc" />
4001+
hello
4002+
</div>
4003+
</body>
4004+
</html>,
4005+
);
4006+
4007+
ReactDOMClient.hydrateRoot(container, <App url="client" />);
4008+
await waitForAll([]);
4009+
expect(getMeaningfulChildren(document)).toEqual(
4010+
<html>
4011+
<head>
4012+
<link rel="preload" as="script" href="client" nonce="abc" />
4013+
</head>
4014+
<body>
4015+
<div id="container">
4016+
<link rel="preload" as="script" href="server" nonce="abc" />
4017+
hello
4018+
</div>
4019+
</body>
4020+
</html>,
4021+
);
4022+
});
39844023
});
39854024

39864025
describe('ReactDOM.preinit(href, { as: ... })', () => {

packages/react-dom/src/shared/ReactDOMTypes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type PreloadOptions = {
1414
crossOrigin?: string,
1515
integrity?: string,
1616
type?: string,
17+
nonce?: string,
1718
fetchPriority?: 'high' | 'low' | 'auto',
1819
};
1920
export type PreinitOptions = {

0 commit comments

Comments
 (0)