import { afterEach, describe, expect, test } from 'vitest' import { act, cleanup, fireEvent, render, screen, waitFor, } from '@testing-library/react' import { createPortal } from 'react-dom' import ReactDOMServer from 'react-dom/server' import { HeadContent, Link, Outlet, RouterProvider, createBrowserHistory, createMemoryHistory, createRootRoute, createRoute, createRouter, } from '../src' import { Scripts } from '../src/Scripts' import type { Manifest } from '@tanstack/router-core' const createTestManifest = (routeId: string) => ({ routes: { [routeId]: { assets: [ { tag: 'link', attrs: { rel: 'stylesheet', href: '/main.css', }, }, ], }, }, }) satisfies Manifest const browserHistories: Array> = [] const createTestBrowserHistory = () => { const history = createBrowserHistory() browserHistories.push(history) return history } afterEach(() => { cleanup() browserHistories.splice(0).forEach((history) => history.destroy()) window.history.replaceState(null, 'root', '/') }) describe('ssr scripts', () => { test('it works', async () => { const rootRoute = createRootRoute({ head: () => { return { scripts: [ { src: 'script.js', }, { src: 'script2.js', }, ], } }, component: () => { return (
root
) }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, head: () => { return { scripts: [ { src: 'script3.js', }, ], } }, }) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), isServer: true, }) await router.load() expect(router.state.matches.map((d) => d.headScripts).flat(1)).toEqual([ { src: 'script.js' }, { src: 'script2.js' }, { src: 'script3.js' }, ]) }) test('excludes `undefined` script values', async () => { const rootRoute = createRootRoute({ scripts: () => [ { src: 'script.js' }, undefined, // 'script2.js' opted out by certain conditions, such as `NODE_ENV=production`. ], component: () => { return (
root
) }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, scripts: () => [{ src: 'script3.js' }], component: () => { return
index
}, }) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), isServer: true, }) await router.load() expect(router.state.matches.map((d) => d.scripts).flat(1)).toEqual([ { src: 'script.js' }, undefined, { src: 'script3.js' }, ]) const { container } = await act(() => render(), ) expect(await screen.findByTestId('root')).toBeInTheDocument() expect(await screen.findByTestId('index')).toBeInTheDocument() expect(container.innerHTML).toEqual( `
root
index
`, ) }) }) describe('scripts with async/defer attributes', () => { test('server renders scripts with async/defer attributes in output', async () => { const rootRoute = createRootRoute({ scripts: () => [ { src: 'script.js', async: true, }, { src: 'script2.js', defer: true, }, ], component: () => { return (
root
) }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, component: () => { return
index
}, }) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), isServer: true, }) await router.load() // Use ReactDOMServer.renderToString to test actual server output const html = ReactDOMServer.renderToString( , ) expect(html).toMatch(/]*src="script\.js"[^>]*async=""/) expect(html).toMatch(/]*src="script2\.js"[^>]*defer=""/) }) test('client renders scripts with attributes (including async/defer)', async () => { const rootRoute = createRootRoute({ scripts: () => [ { src: 'script.js', async: true, crossOrigin: 'anonymous', }, ], component: () => { return (
root
) }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, component: () => { return
index
}, }) // Clear head and any leftover body scripts between tests. document.head.innerHTML = '' document.querySelectorAll('body script').forEach((s) => s.remove()) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), isServer: false, // Client-side rendering }) await router.load() await act(() => render()) expect(await screen.findByTestId('async-root')).toBeInTheDocument() const script = document.querySelector('script[src="script.js"]') expect(script).toBeTruthy() // Attributes are preserved on the client. expect(script?.getAttribute('src')).toBe('script.js') expect(script?.hasAttribute('async')).toBe(true) expect(script?.getAttribute('crossorigin')).toBe('anonymous') }) }) describe('ssr HeadContent', () => { test('derives title, dedupes meta, and allows non-loader HeadContent', async () => { const rootRoute = createRootRoute({ loader: () => new Promise((r) => setTimeout(r, 1)).then(() => ({ description: 'Root', })), head: ({ loaderData }) => { return { meta: [ { title: 'Root', }, { name: 'description', content: loaderData?.description, }, { name: 'image', content: 'image.jpg', }, { property: 'og:image', content: 'root-image.jpg', }, { property: 'og:description', content: 'Root description', }, ], } }, component: () => { return }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, loader: () => new Promise((r) => setTimeout(r, 2)).then(() => ({ description: 'Index', })), head: ({ loaderData }) => { return { meta: [ { title: 'Index', }, { name: 'description', content: loaderData?.description, }, { name: 'last-modified', content: '2021-10-10', }, { property: 'og:image', content: 'index-image.jpg', }, ], } }, }) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), isServer: true, }) await router.load() expect(router.state.matches.map((d) => d.meta).flat(1)).toEqual([ { title: 'Root' }, { name: 'description', content: 'Root' }, { name: 'image', content: 'image.jpg' }, { property: 'og:image', content: 'root-image.jpg' }, { property: 'og:description', content: 'Root description' }, { title: 'Index' }, { name: 'description', content: 'Index' }, { name: 'last-modified', content: '2021-10-10' }, { property: 'og:image', content: 'index-image.jpg' }, ]) const html = ReactDOMServer.renderToString( , ) expect(html).toEqual( `Index`, ) }) test('keeps manifest stylesheet links mounted when history state changes', async () => { const history = createTestBrowserHistory() const rootRoute = createRootRoute({ component: () => { return ( <> {createPortal(, document.head)} ) }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, component: () =>
Index
, }) const router = createRouter({ history, routeTree: rootRoute.addChildren([indexRoute]), }) router.ssr = { manifest: createTestManifest(rootRoute.id), } await router.load() await act(() => render()) const getStylesheetLink = () => Array.from(document.querySelectorAll('link[rel="stylesheet"]')).find( (link) => link.getAttribute('href') === '/main.css', ) await waitFor(() => { expect(getStylesheetLink()).toBeInstanceOf(HTMLLinkElement) }) const initialLink = getStylesheetLink() expect(initialLink).toBeInstanceOf(HTMLLinkElement) fireEvent.click(screen.getByRole('button', { name: 'Replace state' })) await waitFor(() => { expect(router.state.location.state).toMatchObject({ slideId: 'slide-2', }) }) expect(getStylesheetLink()).toBe(initialLink) expect( Array.from(document.querySelectorAll('link[rel="stylesheet"]')).filter( (link) => link.getAttribute('href') === '/main.css', ), ).toHaveLength(1) }) test('keeps manifest stylesheet links mounted across repeated Link navigations', async () => { const history = createTestBrowserHistory() const rootRoute = createRootRoute({ component: () => { return ( <> {createPortal(, document.head)} ) }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, component: () => Go to about page, }) const aboutRoute = createRoute({ path: '/about', getParentRoute: () => rootRoute, component: () => Back to home, }) const router = createRouter({ history, routeTree: rootRoute.addChildren([indexRoute, aboutRoute]), }) router.ssr = { manifest: createTestManifest(rootRoute.id), } await router.load() await act(() => render()) const getStylesheetLink = () => Array.from(document.querySelectorAll('link[rel="stylesheet"]')).find( (link) => link.getAttribute('href') === '/main.css', ) await waitFor(() => { expect(getStylesheetLink()).toBeInstanceOf(HTMLLinkElement) }) const initialLink = getStylesheetLink() expect(initialLink).toBeInstanceOf(HTMLLinkElement) for (let i = 0; i < 5; i++) { fireEvent.click(screen.getByRole('link', { name: 'Go to about page' })) await waitFor(() => { expect(router.state.location.pathname).toBe('/about') }) await screen.findByRole('link', { name: 'Back to home' }) fireEvent.click(screen.getByRole('link', { name: 'Back to home' })) await waitFor(() => { expect(router.state.location.pathname).toBe('/') }) await screen.findByRole('link', { name: 'Go to about page' }) } expect(getStylesheetLink()).toBe(initialLink) expect( Array.from(document.querySelectorAll('link[rel="stylesheet"]')).filter( (link) => link.getAttribute('href') === '/main.css', ), ).toHaveLength(1) }) }) describe('data script rendering', () => { test('data script renders content on server (SSR)', async () => { const jsonLd = JSON.stringify({ '@context': 'https://schema.org', '@type': 'Article', headline: 'Test Article', }) const rootRoute = createRootRoute({ scripts: () => [ { type: 'application/ld+json', children: jsonLd, }, ], component: () => { return (
root
) }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, }) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), isServer: true, }) await router.load() const html = ReactDOMServer.renderToString( , ) expect(html).toContain('application/ld+json') expect(html).toContain(jsonLd) }) test('data script preserves content on client', async () => { const jsonLd = JSON.stringify({ '@context': 'https://schema.org', '@type': 'WebSite', name: 'Test', }) const rootRoute = createRootRoute({ scripts: () => [ { type: 'application/ld+json', children: jsonLd, }, ], component: () => { return (
root
) }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, }) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), }) await router.load() const { container } = await act(() => render(), ) const rootEl = container.querySelector('[data-testid="data-client-root"]') expect(rootEl).not.toBeNull() const scriptEl = container.querySelector( 'script[type="application/ld+json"]', ) expect(scriptEl).not.toBeNull() expect(scriptEl!.innerHTML).toBe(jsonLd) }) test('executable script is injected into document.head via useEffect on client', async () => { const rootRoute = createRootRoute({ scripts: () => [ { children: 'console.log("hello")', }, ], component: () => { return (
root
) }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, }) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), }) await router.load() const { container } = await act(() => render(), ) const rootEl = container.querySelector('[data-testid="exec-root"]') expect(rootEl).not.toBeNull() // Executable inline scripts are injected into document.head via useEffect, // not rendered in the React tree on the client. const headScript = document.head.querySelector( 'script:not([type]):not([src])', ) expect(headScript).not.toBeNull() expect(headScript!.textContent).toBe('console.log("hello")') }) test('module script is injected into document.head via useEffect on client', async () => { const rootRoute = createRootRoute({ scripts: () => [ { type: 'module', children: 'import { foo } from "./foo.js"', }, ], component: () => { return (
root
) }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, }) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), }) await router.load() const { container } = await act(() => render(), ) const rootEl = container.querySelector('[data-testid="module-root"]') expect(rootEl).not.toBeNull() // Module scripts are injected into document.head via useEffect, // not rendered in the React tree on the client. const moduleScript = document.head.querySelector('script[type="module"]') expect(moduleScript).not.toBeNull() expect(moduleScript!.textContent).toBe('import { foo } from "./foo.js"') }) test('application/json data script preserves content', async () => { const jsonData = JSON.stringify({ config: { theme: 'dark' } }) const rootRoute = createRootRoute({ scripts: () => [ { type: 'application/json', children: jsonData, }, ], component: () => { return (
root
) }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, }) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), }) await router.load() const { container } = await act(() => render(), ) const rootEl = container.querySelector('[data-testid="json-root"]') expect(rootEl).not.toBeNull() const scriptEl = container.querySelector('script[type="application/json"]') expect(scriptEl).not.toBeNull() expect(scriptEl!.innerHTML).toBe(jsonData) }) test('data script does not duplicate into document.head', async () => { const jsonLd = JSON.stringify({ '@context': 'https://schema.org', '@type': 'Organization', name: 'Test Org', }) const rootRoute = createRootRoute({ scripts: () => [ { type: 'application/ld+json', children: jsonLd, }, ], component: () => { return (
root
) }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, }) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), }) await router.load() const { container } = await act(() => render(), ) const rootEl = container.querySelector('[data-testid="dup-root"]') expect(rootEl).not.toBeNull() // Data scripts should NOT be duplicated into document.head by useEffect const headScripts = document.head.querySelectorAll( 'script[type="application/ld+json"]', ) expect(headScripts.length).toBe(0) // Should only exist once in the container const containerScripts = container.querySelectorAll( 'script[type="application/ld+json"]', ) expect(containerScripts.length).toBe(1) expect(containerScripts[0]!.innerHTML).toBe(jsonLd) }) test('empty string type is treated as executable, injected via useEffect on client', async () => { const rootRoute = createRootRoute({ scripts: () => [ { type: '', children: 'console.log("empty type")', }, ], component: () => { return (
root
) }, }) const indexRoute = createRoute({ path: '/', getParentRoute: () => rootRoute, }) const router = createRouter({ history: createMemoryHistory({ initialEntries: ['/'], }), routeTree: rootRoute.addChildren([indexRoute]), }) await router.load() const { container } = await act(() => render(), ) const rootEl = container.querySelector('[data-testid="empty-type-root"]') expect(rootEl).not.toBeNull() // Empty type = text/javascript per HTML spec. Executable scripts are // injected into document.head via useEffect on the client. const scriptEl = document.head.querySelector('script[type=""]') expect(scriptEl).not.toBeNull() expect(scriptEl!.textContent).toBe('console.log("empty type")') }) })