Skip to content
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
12 changes: 11 additions & 1 deletion packages/react-router/src/Asset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,17 @@ function Script({
// After hydration, return null — the useEffect handles imperative injection.
if (!hydrated) {
if (attrs?.src) {
return <script {...attrs} suppressHydrationWarning />
if (!preventScriptHoist) {
return <script {...attrs} suppressHydrationWarning />
}

return (
<script
{...attrs}
onLoad={noopScriptHandler}
suppressHydrationWarning
/>
)
}

if (typeof children === 'string') {
Expand Down
77 changes: 77 additions & 0 deletions packages/react-router/tests/Scripts.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
waitFor,
} from '@testing-library/react'
import { createPortal } from 'react-dom'
import { hydrateRoot } from 'react-dom/client'
import ReactDOMServer from 'react-dom/server'
import { hydrate } from '@tanstack/router-core/ssr/client'
import { dehydrateSsrMatchId } from '../../router-core/src/ssr/ssr-match-id'
Expand Down Expand Up @@ -281,6 +282,82 @@ describe('scripts with async/defer attributes', () => {
expect(html).not.toContain('client-entry')
})

test('hydration honors preventScriptHoist for manifest src scripts', async () => {
const clientEntryAsset = {
attrs: {
src: '/hydrate-entry.js',
type: 'module',
async: true,
},
} satisfies NonNullable<Manifest['routes'][string]['scripts']>[number]

const createTestRouter = (isServer: boolean) => {
const rootRoute = createRootRoute({
component: () => {
return (
<div>
<main data-testid="hydrate-content">content</main>
<Outlet />
<Scripts />
</div>
)
},
})

const indexRoute = createRoute({
path: '/',
getParentRoute: () => rootRoute,
})

const router = createRouter({
history: createMemoryHistory({
initialEntries: ['/'],
}),
routeTree: rootRoute.addChildren([indexRoute]),
isServer,
})
router.ssr = {
manifest: {
routes: {
[rootRoute.id]: {
scripts: [clientEntryAsset],
},
},
},
}
return router
}

const serverRouter = createTestRouter(true)
await serverRouter.load()

const html = ReactDOMServer.renderToString(
<RouterProvider router={serverRouter} />,
)
expect(html).toContain('src="/hydrate-entry.js"')

const container = document.createElement('div')
container.innerHTML = html
document.body.appendChild(container)

const clientRouter = createTestRouter(false)
await clientRouter.load()

const onRecoverableError = vi.fn()
let root: ReturnType<typeof hydrateRoot> | undefined
await act(() => {
root = hydrateRoot(container, <RouterProvider router={clientRouter} />, {
onRecoverableError,
})
})

expect(await screen.findByTestId('hydrate-content')).toBeInTheDocument()
expect(onRecoverableError).not.toHaveBeenCalled()

await act(() => root?.unmount())
container.remove()
})

test('client renders scripts with attributes (including async/defer)', async () => {
const rootRoute = createRootRoute({
scripts: () => [
Expand Down