Skip to content

Commit 6653744

Browse files
authored
fix(docs): serve a docs 404 from the prerendered not-found route (#192)
Fixes #182 /docs/no-such-page returned a correct 404 status with an empty body: the 404 markup reached the RSC payload but never the server-rendered HTML, so crawlers and no-JS clients got a blank page while every browser filled it in. Root cause: notFound() raised while a request is rendered dynamically escapes both the RSC and the SSR render. Next builds the response from getErrorRSCPayload, which seeds the document as an empty error shell and ships the real 404 tree only in the payload. No not-found boundary participates in that server render at any depth — measured at the root, in the locale segment, with the shell relocated above the boundary, and via the experimental global-not-found convention. A boundary in the locale segment is byte-for-byte a no-op, which is why this is a routing flag and not a new not-found.tsx. dynamicParams = false routes an unknown docs slug to the prerendered /_not-found route — an ordinary page render, which is why /no-such-page always worked. It removes the dynamic render the boundary cannot survive. Cost: a page not in the enumeration needs a rebuild to appear. Every page here comes from local MDX that generateStaticParams already enumerates, so that is true in practice today; the OG route sets the same flag for the same reason. Verified on a same-base A/B: control errorshell=TRUE with 0 occurrences of the 404 copy in markup, branch errorshell=FALSE with the copy present, across the en/zh-Hans/ja and nested variants. /no-such-page unchanged, static count 577 on both sides, redirects intact, next dev matching. A docs 404 still declares en rather than the requested locale; that half is #191.
1 parent b078736 commit 6653744

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

  • apps/docs/app/[lang]/docs/[[...slug]]

‎apps/docs/app/[lang]/docs/[[...slug]]/page.tsx‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,40 @@ export default async function Page(props: {
268268
);
269269
}
270270

271+
/**
272+
* Only the pages `generateStaticParams` enumerates are servable; every other
273+
* slug is answered by the prerendered `/_not-found` route.
274+
*
275+
* This is what makes a docs 404 render at all. `notFound()` raised while a
276+
* request is being rendered *dynamically* escapes both the RSC and the SSR
277+
* render; Next catches it in the SSR error handler and builds the response from
278+
* `getErrorRSCPayload`, which seeds the document as an empty error shell and
279+
* ships the real 404 tree only in the RSC payload, for the client to render
280+
* after hydration. No `not-found` boundary participates in that server render —
281+
* not at the root, not in the locale segment, not beside the page. Measured at
282+
* every one of those placements, and a boundary added in the locale segment is
283+
* byte-for-byte a no-op, which is why this fix is a routing flag and not a new
284+
* `not-found.tsx`.
285+
*
286+
* So the 404 has to be reached by the path that already works. An unmatched URL
287+
* is served by `/_not-found`, an ordinary prerendered page render, which is why
288+
* `/no-such-page` has always rendered its body server-side while
289+
* `/docs/no-such-page` served a blank one. `dynamicParams = false` routes an
290+
* unknown docs slug there too, instead of rendering this page so it can throw.
291+
* Not a workaround for the boundary: it removes the dynamic render that the
292+
* boundary cannot survive.
293+
*
294+
* The behavioural cost, stated plainly: a page that is not in the enumeration
295+
* needs a rebuild to appear, rather than rendering on demand. Every page here
296+
* comes from local MDX that `generateStaticParams` already enumerates, so that
297+
* is true in practice today — this makes it explicit rather than introducing
298+
* it. `app/og/docs/[...slug]/route.tsx` sets the same flag for the same reason.
299+
*
300+
* `notFound()` below stays: it is still what answers a slug that is enumerated
301+
* but unresolvable, and it is the type-level narrowing for `page`.
302+
*/
303+
export const dynamicParams = false;
304+
271305
export async function generateStaticParams() {
272306
return source.generateParams();
273307
}

0 commit comments

Comments
 (0)