Skip to content
Source
Sign inSign up
TanStack/router

fix(router-core): read ssr.nonce lazily for hydration scripts #8859

Openaliassar(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/8859/headopened

Showing 5 changed files with 15 additions and 7 deletions (loading 2).

x
0 of 5 viewed, 5 left
5 additions and 0 deletions.changeset/late-ssr-nonce-hydration.mdAdded
View file
@@ -0,0 +1,5 @@
+---
+'@tanstack/router-core': patch
+---
+​
+Read `router.options.ssr.nonce` when the hydration scripts are built, so a nonce set after the router is created reaches the hydration `<script>` tags.
7 additions and 6 deletionspackages/router-core/src/ssr/hydrationScripts.tsModified
View file
@@ -178,7 +178,7 @@
private opening: string | undefined
​
constructor(
- private readonly nonce: string | undefined,
+ private readonly getNonce: (() => string | undefined) | undefined,
initialSources?: ReadonlyArray<string>,
) {
// ServerSsr exposes this method as a bare callback, so keep this single
@@ -450,8 +450,9 @@
​
private pullReady() {
- const scriptOpening = (this.opening ??= this.nonce
- ? `<script nonce="${escapeAttribute(this.nonce)}">`
+ const nonce = this.getNonce?.()
+ const scriptOpening = (this.opening ??= nonce
+ ? `<script nonce="${escapeAttribute(nonce)}">`
: '<script>')
let codeUnits = scriptOpening.length + DYNAMIC_CLOSE_SOURCE.length
let batchLength = 0