An empty 404 in Next.js 16: where it comes from and how we got around it
A missing address on our site answered with a 404 code but showed a blank screen without JavaScript. Where the __next_error__ shell comes from, and how we serve a prebuilt 404 from the proxy without weakening the CSP.
Our site is built with Next.js 16 and the App Router. Every public page is generated in advance, at build time. The Content Security Policy (CSP) allows inline scripts only by their hashes, without unsafe-inline. Languages live in the app/[locale] segment through next-intl: /en, /ru, /uz.
During a manual check with JavaScript turned off, we opened an address that does not exist. The server answered with a 404 code, and the page was blank. Production did the same.
What Next serves
For an address inside app/[locale] that is not among the generated pages, notFound() fires. There is no ready HTML for such an address, so Next renders the response on request. This is what arrives (shortened):
<html id="__next_error__">
<head>
…
</head>
<body>
<div hidden=""><!--$--><!--/$--></div>
<script>
…
</script>
<!-- six more scripts -->
</body>
</html>
There is no page markup in <body>. The text "Page not found" exists only inside the scripts, in the React Server Components data, and the browser puts it on screen once it has run the JavaScript. We reproduced this in a minimal app on Next 15.5.29, 16.4.5 and 16.4.6: the same everywhere, so an upgrade will not help.
For addresses outside the dynamic segment, Next has its own prebuilt _not-found.html. It does not build a separate 404 for each language inside [locale].
Why this is bad
- While the scripts load, a person sees a white screen. With JavaScript turned off, or when loading fails, it stays white.
- A search crawler that does not run JavaScript sees an empty page.
- For the CSP it is an exception to the rules. A generated page has its script hashes known in advance. A page rendered on request gets a new nonce for every response, and the proxy has to issue it.
The 404 is generated in advance too
The first step is an ordinary page, app/[locale]/404/page.tsx, with noindex. Next builds it for each language like any other page: .next/server/app/ru/404.html holds complete HTML with the text.
The second step is the proxy (what Next 16 calls middleware). It decides who gets this HTML. The rule: for a browser, an address under a language that is not in .next/prerender-manifest.json is a 404.
export function staticNotFoundLocale(
pathname: string,
prerendered: ReadonlySet<string>,
): AppLocale | null {
const locale = LOCALE_PREFIX.exec(pathname)?.[1] as AppLocale | undefined;
if (!locale) return null; // outside the languages: next-intl redirects
if (pathname === notFoundPath(locale)) return locale; // /ru/404 itself is a 404 too
return prerendered.has(pathname) ? null : locale;
}
We build the response from the ready file (shortened):
async function staticNotFound(request: NextRequest): Promise<Response | null> {
if (!request.headers.get('accept')?.includes('text/html')) return null;
if (request.method !== 'GET' && request.method !== 'HEAD') return null;
const routes = await prerenderedRoutes();
const locale = routes && staticNotFoundLocale(request.nextUrl.pathname, routes);
if (!locale) return null;
const html = await notFoundDocuments[locale]();
if (html === null) return null;
return new Response(request.method === 'HEAD' ? null : html, {
status: 404,
headers: {
'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': 'private, no-cache, no-store, max-age=0, must-revalidate',
'Content-Security-Policy': buildCsp({ inlineScriptHashes: inlineScriptHashes(html), dev }),
},
});
}
The inline script hashes are computed from the same HTML that goes into the response. The CSP stays strict: no nonce and no unsafe-inline.
What stays with Next
Navigation inside the site. The Next router requests data with Accept: */* and hides its own headers (RSC, _rsc) from the proxy. Such requests do not pass the text/html check and go on to Next: it answers with React Server Components data and renders not-found.tsx, as before.
Requests without text/html. curl or a script gets Next's on-request response, with a nonce in the CSP.
Failure to read the files. If the proxy cannot read the manifest or the 404 HTML, it logs the error once and passes the request on. The site works as it did before the change.
How we check it
- Playwright. With JavaScript turned off, a missing address answers with a 404 code and shows the heading. Each language gets the generated document, with no
__next_error__shell./ru/404itself also answers 404. A router request with theRSCheader is still served by Next. - The Docker image in CI. The same request goes to the built container. The proxy reads the files from the standalone build. If they are missing there, the on-request response with a nonce comes back, and the check fails before release.
- After release. The same requests go to the site through Cloudflare.
What we pay for it
The proxy reads internal files of the Next build: prerender-manifest.json and server/app/*.html. This is not a public API, and their format may change with a new version of Next. We would learn about it from the image check in CI, before release.
The rule is strict: any address under a language that is not in the list of generated pages reaches a browser as a 404. If a public page rendered on request appears, the rule will need a change. For a site where every page is static, that suits us.