Skip to content

Commit a255e9d

Browse files
fix: nonce support (#5287)
1 parent 7a129f7 commit a255e9d

6 files changed

Lines changed: 29 additions & 8 deletions

File tree

packages/react-router/src/Asset.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ export function Asset({
1212
tag,
1313
attrs,
1414
children,
15-
}: RouterManagedTag): React.ReactElement | null {
15+
nonce,
16+
}: RouterManagedTag & { nonce?: string }): React.ReactElement | null {
1617
switch (tag) {
1718
case 'title':
1819
return (
@@ -23,16 +24,21 @@ export function Asset({
2324
case 'meta':
2425
return <meta {...attrs} suppressHydrationWarning />
2526
case 'link':
26-
return <link {...attrs} suppressHydrationWarning />
27+
return <link {...attrs} nonce={nonce} suppressHydrationWarning />
2728
case 'style':
2829
return (
2930
<style
3031
{...attrs}
3132
dangerouslySetInnerHTML={{ __html: children as string }}
33+
nonce={nonce}
3234
/>
3335
)
3436
case 'script':
35-
return <Script attrs={attrs}>{children}</Script>
37+
return (
38+
<Script attrs={attrs} nonce={nonce}>
39+
{children}
40+
</Script>
41+
)
3642
default:
3743
return null
3844
}
@@ -41,9 +47,11 @@ export function Asset({
4147
function Script({
4248
attrs,
4349
children,
50+
nonce,
4451
}: {
4552
attrs?: ScriptAttrs
4653
children?: string
54+
nonce?: string
4755
}) {
4856
const router = useRouter()
4957

@@ -146,7 +154,7 @@ function Script({
146154
}
147155

148156
if (attrs?.src && typeof attrs.src === 'string') {
149-
return <script {...attrs} suppressHydrationWarning />
157+
return <script {...attrs} suppressHydrationWarning nonce={nonce} />
150158
}
151159

152160
if (typeof children === 'string') {
@@ -155,6 +163,7 @@ function Script({
155163
{...attrs}
156164
dangerouslySetInnerHTML={{ __html: children }}
157165
suppressHydrationWarning
166+
nonce={nonce}
158167
/>
159168
)
160169
}

packages/react-router/src/HeadContent.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ export const useTags = () => {
174174
*/
175175
export function HeadContent() {
176176
const tags = useTags()
177+
const router = useRouter()
178+
const nonce = router.options.ssr?.nonce
177179
return tags.map((tag) => (
178-
<Asset {...tag} key={`tsr-meta-${JSON.stringify(tag)}`} />
180+
<Asset {...tag} key={`tsr-meta-${JSON.stringify(tag)}`} nonce={nonce} />
179181
))
180182
}
181183

packages/react-router/src/Scripts.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ export const Scripts = () => {
5858
return (
5959
<>
6060
{allScripts.map((asset, i) => (
61-
<Asset {...asset} key={`tsr-scripts-${asset.tag}-${i}`} />
61+
<Asset
62+
{...asset}
63+
key={`tsr-scripts-${asset.tag}-${i}`}
64+
nonce={router.options.ssr?.nonce}
65+
/>
6266
))}
6367
</>
6468
)

packages/react-router/src/ssr/renderRouterToStream.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const renderRouterToStream = async ({
2323
if (typeof ReactDOMServer.renderToReadableStream === 'function') {
2424
const stream = await ReactDOMServer.renderToReadableStream(children, {
2525
signal: request.signal,
26+
nonce: router.options.ssr?.nonce,
2627
})
2728

2829
if (isbot(request.headers.get('User-Agent'))) {
@@ -44,6 +45,7 @@ export const renderRouterToStream = async ({
4445

4546
try {
4647
const pipeable = ReactDOMServer.renderToPipeableStream(children, {
48+
nonce: router.options.ssr?.nonce,
4749
...(isbot(request.headers.get('User-Agent'))
4850
? {
4951
onAllReady() {

packages/solid-router/src/ssr/renderRouterToStream.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ export const renderRouterToStream = async ({
1818
}) => {
1919
const { writable, readable } = new TransformStream()
2020

21-
const stream = Solid.renderToStream(children)
21+
const stream = Solid.renderToStream(children, {
22+
nonce: router.options.ssr?.nonce,
23+
})
2224

2325
if (isbot(request.headers.get('User-Agent'))) {
2426
await stream

packages/solid-router/src/ssr/renderRouterToString.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export const renderRouterToString = async ({
1212
children: () => JSXElement
1313
}) => {
1414
try {
15-
let html = Solid.renderToString(children)
15+
let html = Solid.renderToString(children, {
16+
nonce: router.options.ssr?.nonce,
17+
})
1618
router.serverSsr!.setRenderFinished()
1719
const injectedHtml = await Promise.all(router.serverSsr!.injectedHtml).then(
1820
(htmls) => htmls.join(''),

0 commit comments

Comments
 (0)