The prerendered 404 page is served for other URLs, so its dehydrated match id differs and the router renders the catch-all's pending component first. Without one that render was empty, which gave React error 418 and duplicate head tags. The tests run the built output in a real browser under the production CSP. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
71 lines
2.5 KiB
TypeScript
71 lines
2.5 KiB
TypeScript
// @vitest-environment node
|
|
import { ChakraProvider } from '@chakra-ui/react';
|
|
import {
|
|
createMemoryHistory,
|
|
createRouter,
|
|
HeadContent,
|
|
RouterProvider,
|
|
rootRouteId,
|
|
} from '@tanstack/react-router';
|
|
import type { ReactNode } from 'react';
|
|
import { prerender } from 'react-dom/static';
|
|
import { describe, expect, it } from 'vitest';
|
|
import siteContent from './content/siteContent';
|
|
import { emailAddress } from './lib/emailAddress';
|
|
import { routerOptions } from './router';
|
|
import sitePages from './sitePages';
|
|
import system from './theme';
|
|
|
|
// Fast guard against `window` or `document` in a render path (BR-TST-07):
|
|
// every page is rendered in Node, where either one throws, before a full
|
|
// build is needed to find out. The build's own prerender stays authoritative.
|
|
// The document shell is left out as in renderApp: it renders <html>, and what
|
|
// is guarded here is the page inside it.
|
|
const TestShell = ({ children }: { children: ReactNode }) => (
|
|
<>
|
|
<HeadContent />
|
|
{children}
|
|
</>
|
|
);
|
|
|
|
// Waits for lazy chunks like the real prerender (the form on /contact)
|
|
async function prerenderPath(path: string) {
|
|
const router = createRouter({
|
|
...routerOptions(),
|
|
history: createMemoryHistory({ initialEntries: [path] }),
|
|
});
|
|
const rootOptions = router.routesById[rootRouteId].options as {
|
|
shellComponent?: typeof TestShell;
|
|
};
|
|
rootOptions.shellComponent = TestShell;
|
|
await router.load();
|
|
const { prelude } = await prerender(
|
|
<ChakraProvider value={system}>
|
|
<RouterProvider router={router} />
|
|
</ChakraProvider>,
|
|
);
|
|
return new Response(prelude).text();
|
|
}
|
|
|
|
describe('prerender in Node', () => {
|
|
it.each([...Object.keys(sitePages), '/onbekend'])(
|
|
'renders %s without window or document',
|
|
async (path) => {
|
|
const html = await prerenderPath(path);
|
|
expect(html).not.toBe('');
|
|
expect(html).toMatch(/<h1[\s>]/);
|
|
},
|
|
30_000,
|
|
);
|
|
|
|
it('keeps the assembled address out of the HTML of /contact', async () => {
|
|
const address = emailAddress(siteContent.email);
|
|
expect(address).toBeTruthy();
|
|
const html = await prerenderPath('/contact');
|
|
// The form is in the HTML, so the check covers the part that renders
|
|
// the address after a failed send (BR-MAIL-06)
|
|
expect(html).toContain('data-testid="contact-form"');
|
|
expect(html.toLowerCase()).not.toContain(address!.toLowerCase());
|
|
});
|
|
});
|