Repository navigation
fix(react-router): suppress nonce hydration warning in <Links> - #15721
Conversation
|
I've opened #15721 with a fix and a regression test. |
|
The default root error boundary has the same mismatch, and this PR doesn't cover it. When a loader throws and the app <pa error={Error}>
<ma title="Applicatio..." isOutsideRemixApp={undefined}>
<h1>
<pre>
<script
+ nonce={undefined}
- nonce=""Repro, framework mode with
With this PR's change applied, react-router 8.5.1, react 19.3.1, Chromium 156. |
|
Thanks for catching this! You're right: I pushed a commit that adds |
Replace the original hydration tests with the council-reviewed cases for critical CSS, route links, default developer scripts and page prefetches. Add browser coverage for hydration and nonce preservation, and clarify the change file. The runtime fix is already present in PR remix-run#15721.
|
Pushed c5c00db to strengthen the regression coverage around the existing fix:
I also tested whether carrying the document nonce into client
With CSP enabled, the browser exposes the value through This supports keeping the existing suppression approach. The experiment injected client context directly; it did not implement nonce transport in The full repository suites were not rerun after replaying the follow-up commit onto this PR's head; the isolated browser experiment used the resulting commit's component source. Authored by an AI agent on behalf of @brophdawg11 |
Bring PR remix-run#15721 up to date with main at e6978a6.
Include main at 6abdb19, which advanced during merge validation.
brophdawg11
left a comment
There was a problem hiding this comment.
Reviewed 956b64a. The six-file PR diff retains the reviewed nonce fix and regression coverage after merging main. The Chromium matrix confirmed that matching client context alone does not eliminate nonce hydration warnings under CSP, while suppression preserves DOM identity and nonce values. No code blockers found; merge remains contingent on CI.
Authored by an AI agent on behalf of @brophdawg11
Fixes #15603
Since #15170 (v7.18.0),
<Links>falls back to the<ServerRouter nonce>value fromFrameworkContext, so the server rendersnonce="..."on the critical CSS<style>/<link>and on route<link>s. In the browser,<HydratedRouter>doesn't provide a nonce, so<Links>rendersnonce={undefined}and React logs a hydration mismatch:<Links> <link data-react-router-critical-css="" rel="stylesheet" href="/@react-router/critical.css?pathname=/" + nonce={undefined} - nonce="" >This happens with the setup from the Security guide (nonce on
<ServerRouter>andrenderToPipeableStreamonly), on React 19.2 as well as 19.3.<Scripts>and<ScrollRestoration>have the same server/client difference, but their elements already usesuppressHydrationWarning(e.g. the<link rel="modulepreload" nonce={scriptProps.nonce} suppressHydrationWarning />elements in<Scripts>), so only<Links>warns.Changes
<Links>: addsuppressHydrationWarningto the critical CSS<style>, the critical CSS<link>, and the route<link>elements, matching<Scripts>and<ScrollRestoration>. The server-rendered nonce is unchanged and stays on the elements.components-test.tsx: renders<Links />withrenderToStringusing aFrameworkContextwith a nonce, then hydrates it with a context without a nonce and asserts noconsole.error. It fails without this change (the samenonce={undefined}/nonce="server-nonce"diff for<style>and<link>) and passes with it.suppressHydrationWarningto the dev-warning<script nonce>inRemixRootDefaultErrorBoundaryandRemixRootDefaultHydrateFallback. They have the same mismatch (reported in the review). Covered byerror-boundaries-test.tsx, where both tests fail without the change and pass with it.packages/react-router/.changes/patch.links-nonce-hydration-warning.md.Alternative
Another option would be to provide the nonce on the client (for example a
nonceprop on<HydratedRouter>, or reading.noncefrom an existing element) so the client renders the same value. That's a larger API change, so this PR follows the existing<Scripts>approach. Happy to switch if you'd prefer that direction.Testing
pnpm test packages/react-router/__tests__/dom/ssr/components-test.tsx packages/react-router/__tests__/dom/ssr/error-boundaries-test.tsx: 27/27 pass (the new tests fail onmain).pnpm typecheck,pnpm run changes:validateand Prettier pass;pnpm linthas no errors.pnpm test packages/react-router/locally on Windows, two unrelated suites (data-browser-router-test.tsx,special-characters-test.tsx) hit timing/timeouts under parallel load; the rest pass.AI disclosure: I used an AI coding assistant to help prepare this change. I reproduced the issue with the linked repro, reviewed the diff line by line, and confirmed the new test fails without the fix and passes with it.