Skip to content

Commit e38b2bf

Browse files
authored
fix(docs): keep the 404 title after hydration via a metadata export (#219)
The served 404 carried two title elements: the component's own, then Next's metadata title from the root layout's title.default. The browser took the first while parsing; on hydration React re-inserted the metadata title ahead of it and the tab read plain ObjectOS -- a bookmark or history entry for a dead link was indistinguishable from the site index. next-app-loader installs not-found.tsx as the page module of /_not-found rather than as an error boundary, so collectMetadata reads a metadata export here through the ordinary getLayoutOrPageModule path. Confirmed from node_modules and by a sentinel probe read out of the prerendered document. title.absolute rather than a plain string: the root layout declares title.template, which would otherwise run the 404 title through it and reintroduce the drift in a new spelling. The export derives from the same constant that feeds the title element so the two cannot diverge. This also repairs #213's title line, which the same mechanism had been reverting since it landed -- /zh-Hans/no-such-page showed the localized title at parse time and ObjectOS after. #213's lang attribute and body copy were unaffected and are untouched here. After hydration the title is English in every locale: this boundary sits above the [lang] segment, and reading the locale dynamically costs 577 prerendered documents. Not taken. /_not-found still Static, 577 prerendered files, 996/996. One stripped-body hash across thirteen unmatched URLs, +26 bytes versus main -- exactly the title difference. Healthy controls byte-identical. Closes #212
1 parent e8b2062 commit e38b2bf

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

‎apps/docs/app/not-found.tsx‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { i18n } from '@/lib/i18n';
2+
import type { Metadata } from 'next';
23

34
/**
45
* 404 shell for routes that are not under the `[lang]` segment.
@@ -87,6 +88,39 @@ const COPY: Record<string, string> = {
8788
ko: '이 페이지를 찾을 수 없습니다.',
8889
};
8990

91+
/**
92+
* The document title, used in two places that have to agree.
93+
*
94+
* The title element below is what the browser shows while the document parses.
95+
* The `metadata` export is what Next's metadata system resolves for this route,
96+
* and that is what stands once React hydrates — without it the root layout's
97+
* `title.default` (`ObjectOS`) wins the moment hydration completes, and a
98+
* bookmark or a history entry for a dead link reads exactly like the site
99+
* index. Both are fed from this one constant so they cannot drift apart.
100+
*
101+
* `absolute` is required: the root layout declares `title.template`
102+
* (`%s | ObjectOS`), and an ordinary string title would be run through it,
103+
* putting the hydrated title out of step with the parsed one again.
104+
*/
105+
const TITLE = `404: ${COPY.en}`;
106+
107+
/**
108+
* This is a static export and stays one — the route must not acquire a dynamic
109+
* API. `not-found.tsx` is installed by `next-app-loader` as the *page* module
110+
* of the `/_not-found` route, which is why an export here is read at all; it is
111+
* resolved at build time, and the page stays `○` prerendered.
112+
*
113+
* The title is English for every locale. This boundary sits above the `[lang]`
114+
* segment and cannot read the locale (see the note above on why reading it
115+
* dynamically is not viable), so a localized hydrated title is not reachable
116+
* without moving correctness into a script. The script below still localizes
117+
* the title at parse time; after hydration every locale reads the English 404
118+
* title, which is the same fallback the copy table already applies.
119+
*/
120+
export const metadata: Metadata = {
121+
title: { absolute: TITLE },
122+
};
123+
90124
/**
91125
* Inlined verbatim into a `script` element, so it must stay free of anything
92126
* that could close that element early. Every value it embeds is a compile-time
@@ -112,7 +146,7 @@ export default function NotFound() {
112146
return (
113147
<html lang={i18n.defaultLanguage} suppressHydrationWarning>
114148
<body className="flex flex-col min-h-screen">
115-
<title>404: This page could not be found.</title>
149+
<title>{TITLE}</title>
116150
<div
117151
style={{
118152
fontFamily:

0 commit comments

Comments
 (0)