Skip to content

fix: nonce for ReactDOM.preloadModule #33120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFloat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6290,6 +6290,45 @@ body {
' in App (at **)',
]);
});

it('supports nonce', async () => {
function App({url}) {
ReactDOM.preloadModule(url, {as: 'script', nonce: 'abc'});
return 'hello';
}

await act(() => {
renderToPipeableStream(<App url="server" />).pipe(writable);
});

expect(getMeaningfulChildren(document)).toEqual(
<html>
<head />
<body>
<div id="container">
<link rel="modulepreload" href="server" nonce="abc" />
hello
</div>
</body>
</html>,
);

ReactDOMClient.hydrateRoot(container, <App url="client" />);
await waitForAll([]);
expect(getMeaningfulChildren(document)).toEqual(
<html>
<head>
<link rel="modulepreload" href="client" nonce="abc" />
</head>
<body>
<div id="container">
<link rel="modulepreload" href="server" nonce="abc" />
hello
</div>
</body>
</html>,
);
});
});

describe('ReactDOM.preinit(href, { as: ... })', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/react-dom/src/shared/ReactDOMFloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ export function preloadModule(href: string, options?: ?PreloadModuleOptions) {
typeof options.integrity === 'string'
? options.integrity
: undefined,
nonce: typeof options.nonce === 'string' ? options.nonce : undefined,
});
} else {
ReactDOMSharedInternals.d /* ReactDOMCurrentDispatcher */
Expand Down
Loading