Skip to content
Merged
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
17 changes: 13 additions & 4 deletions packages/react-router/src/Asset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export function Asset({
tag,
attrs,
children,
}: RouterManagedTag): React.ReactElement | null {
nonce,
}: RouterManagedTag & { nonce?: string }): React.ReactElement | null {
switch (tag) {
case 'title':
return (
Expand All @@ -23,16 +24,21 @@ export function Asset({
case 'meta':
return <meta {...attrs} suppressHydrationWarning />
case 'link':
return <link {...attrs} suppressHydrationWarning />
return <link {...attrs} nonce={nonce} suppressHydrationWarning />
case 'style':
return (
<style
{...attrs}
dangerouslySetInnerHTML={{ __html: children as string }}
nonce={nonce}
/>
)
case 'script':
return <Script attrs={attrs}>{children}</Script>
return (
<Script attrs={attrs} nonce={nonce}>
{children}
</Script>
)
default:
return null
}
Expand All @@ -41,9 +47,11 @@ export function Asset({
function Script({
attrs,
children,
nonce,
}: {
attrs?: ScriptAttrs
children?: string
nonce?: string
}) {
const router = useRouter()

Expand Down Expand Up @@ -146,7 +154,7 @@ function Script({
}

if (attrs?.src && typeof attrs.src === 'string') {
return <script {...attrs} suppressHydrationWarning />
return <script {...attrs} suppressHydrationWarning nonce={nonce} />
}

if (typeof children === 'string') {
Expand All @@ -155,6 +163,7 @@ function Script({
{...attrs}
dangerouslySetInnerHTML={{ __html: children }}
suppressHydrationWarning
nonce={nonce}
/>
)
}
Expand Down
4 changes: 3 additions & 1 deletion packages/react-router/src/HeadContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ export const useTags = () => {
*/
export function HeadContent() {
const tags = useTags()
const router = useRouter()
const nonce = router.options.ssr?.nonce
return tags.map((tag) => (
<Asset {...tag} key={`tsr-meta-${JSON.stringify(tag)}`} />
<Asset {...tag} key={`tsr-meta-${JSON.stringify(tag)}`} nonce={nonce} />
))
}

Expand Down
6 changes: 5 additions & 1 deletion packages/react-router/src/Scripts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ export const Scripts = () => {
return (
<>
{allScripts.map((asset, i) => (
<Asset {...asset} key={`tsr-scripts-${asset.tag}-${i}`} />
<Asset
{...asset}
key={`tsr-scripts-${asset.tag}-${i}`}
nonce={router.options.ssr?.nonce}
/>
))}
</>
)
Expand Down
2 changes: 2 additions & 0 deletions packages/react-router/src/ssr/renderRouterToStream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const renderRouterToStream = async ({
if (typeof ReactDOMServer.renderToReadableStream === 'function') {
const stream = await ReactDOMServer.renderToReadableStream(children, {
signal: request.signal,
nonce: router.options.ssr?.nonce,
})

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

try {
const pipeable = ReactDOMServer.renderToPipeableStream(children, {
nonce: router.options.ssr?.nonce,
...(isbot(request.headers.get('User-Agent'))
? {
onAllReady() {
Expand Down
4 changes: 3 additions & 1 deletion packages/solid-router/src/ssr/renderRouterToStream.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export const renderRouterToStream = async ({
}) => {
const { writable, readable } = new TransformStream()

const stream = Solid.renderToStream(children)
const stream = Solid.renderToStream(children, {
nonce: router.options.ssr?.nonce,
})

if (isbot(request.headers.get('User-Agent'))) {
await stream
Expand Down
4 changes: 3 additions & 1 deletion packages/solid-router/src/ssr/renderRouterToString.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export const renderRouterToString = async ({
children: () => JSXElement
}) => {
try {
let html = Solid.renderToString(children)
let html = Solid.renderToString(children, {
nonce: router.options.ssr?.nonce,
})
router.serverSsr!.setRenderFinished()
const injectedHtml = await Promise.all(router.serverSsr!.injectedHtml).then(
(htmls) => htmls.join(''),
Expand Down
Loading