Adds browser tests and fixes hydration of the 404 page
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>
This commit is contained in:
+16
-1
@@ -1,9 +1,24 @@
|
||||
import { createFileRoute } from '@tanstack/react-router';
|
||||
import NotFound from '../components/StatusPage/NotFound';
|
||||
|
||||
// Eén component voor component en pendingComponent: de code splitter laat de
|
||||
// import van NotFound vallen als die in een gesplitste (component) en in een
|
||||
// niet-gesplitste eigenschap (pendingComponent) staat.
|
||||
const CatchAll = () => <NotFound />;
|
||||
|
||||
// Vangt elke onbekende URL op en wordt als 404.html voorgerenderd (BR-404-01).
|
||||
// Geen head() en geen notFound(): Start laat de build falen bij een niet-OK
|
||||
// antwoord, en NotFound zet zelf titel en noindex.
|
||||
//
|
||||
// pendingComponent is hier geen laadscherm maar de eerste browserrender. De
|
||||
// HTML is gemaakt voor /404 en de match-id bevat het pad (#$#404), dus voor
|
||||
// /onbekend of /404.html herkent Start de match niet en laat die in de browser
|
||||
// renderen tot het laden klaar is, met pendingComponent als inhoud. Zonder
|
||||
// pendingComponent is dat niets, terwijl de HTML de pagina toont. React
|
||||
// hydrateert die grens los van de rest en geeft fout #418 als het laden dan
|
||||
// nog niet klaar is (trage verbinding of processor). Met dezelfde pagina als
|
||||
// pendingComponent is de eerste render gelijk aan de HTML.
|
||||
export const Route = createFileRoute('/$')({
|
||||
component: NotFound,
|
||||
component: CatchAll,
|
||||
pendingComponent: CatchAll,
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { screen, waitFor, within } from '@testing-library/react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { FakeIntersectionObserver } from '@/test/fakeIntersectionObserver';
|
||||
import { hydrateApp } from '@/test/hydrate';
|
||||
import { hydrateApp, hydrateAppAsStart } from '@/test/hydrate';
|
||||
|
||||
// A direct load of a page with a search: the HTML was prerendered without it,
|
||||
// so the first browser render must ignore it and the page applies it right
|
||||
@@ -71,3 +71,33 @@ describe('homepage ?pakket= on a direct load', () => {
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
// The prerendered 404 page is made for /404 and served for any unknown URL.
|
||||
// Start does not recognise the catch-all match of another path (its id holds
|
||||
// the path), so the browser renders that match itself. Its first render must
|
||||
// be the page the HTML shows, or React reports a mismatch (BR-404-05, V12).
|
||||
describe('404 page served for another URL', () => {
|
||||
it.each(['/onbekend', '/404.html', '/over-mij/x'])(
|
||||
'hydrates %s without a mismatch',
|
||||
async (url) => {
|
||||
const { errors } = await hydrateAppAsStart('/404', url);
|
||||
expect(
|
||||
await screen.findByRole('heading', {
|
||||
name: 'Pagina niet gevonden',
|
||||
}),
|
||||
).toBeInTheDocument();
|
||||
expect(errors).toEqual([]);
|
||||
},
|
||||
);
|
||||
|
||||
it('shows the section buttons after hydrating under /soorten-fotoshoots/', async () => {
|
||||
const { errors } = await hydrateAppAsStart(
|
||||
'/404',
|
||||
'/soorten-fotoshoots/onbekend',
|
||||
);
|
||||
expect(
|
||||
await screen.findByTestId('status-page-section-link'),
|
||||
).toBeInTheDocument();
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user