Angular 22 builds this site with
outputMode: "static": every route
becomes finished HTML at build time, and after that there is no Node server, only files.
For a handful of posts that's exactly right. It only moves one question somewhere I
wasn't looking.
Two halves of an answer
A static host answers an unknown URL with 404.html. Angular's prerenderer writes that
page as 404/index.html, where neither GitLab Pages nor Netlify nor an nginx with
error_page 404 /404.html goes looking for it, so a build step copies it into place
(tools/make-404.mjs). I checked that against a real static server:
curl -s -o /dev/null -w '%{http_code}' http://127.0.0.1:4422/gibt-es-nicht
# 404Status 404, right text, right layout. That's where I would have ticked it off.
But the file that gets served isn't the end of it. It carries the whole application, and once that has loaded the router takes over. It looks at the address bar and decides all over again what belongs on screen.
The line that cost me
{ path: '**', redirectTo: '' }That looks harmless, and in an ordinary single-page app it is. Not here. / isn't a home
page but the language gate. Without JavaScript it stays put, a page offering both
languages; with it, the page forwards to /de/ or /en/ straight away.
So the chain ran like this. The host serves 404.html for /gibt-es-nicht, with
status 404. Angular hydrates. The router sees /gibt-es-nicht, finds only the
catch-all, redirects to /, and the language gate forwards to whichever language the
browser prefers, /de/ in my case. Anyone who followed a dead link lands on the home
page instead and never finds out that the page they wanted doesn't exist.
A prerendered page has to survive the framework waking up. What the server hands over is only the first half of the answer.
Why it stayed invisible for so long
The dev server has no 404.html. You type a broken URL there, end up on the home page
and think nothing of it, because that's exactly what an SPA fallback does. The difference
only shows in a built site, and I had been building to check that everything got
prerendered. I never asked what happened after that.
The fix is a few lines. The catch-all now renders the same page as /404, inside the
shared layout, instead of redirecting anywhere.
{
path: '**',
component: SiteLayoutComponent,
data: { locale: DEFAULT_LOCALE },
children: [{ path: '**', component: NotFoundPage, data: { locale: DEFAULT_LOCALE } }],
}What holds it there is a test that checks the order I'd been missing: open an unknown URL, wait for Angular to take over, and only then look at what's on screen.
await page.goto('/gibt-es-weder-hier-noch-dort');
await expect(page.getByRole('heading', { level: 1 })).toHaveText('Diese Seite gibt es nicht.');
await expect(page.locator('header')).toBeVisible();
expect(new URL(page.url()).pathname).toBe('/gibt-es-weder-hier-noch-dort');The last line is the one that matters. A test that only checked the heading could have passed before the fix as well, as long as it looked quickly enough.
Since then prerendered HTML has only been the starting state for me. Every page the build emits gets two questions: what does it say, and what does the router make of it once it wakes up.