Skip to content

Commit 8feb90d

Browse files
authored
fix(docs): derive the document language from the locale route segment (#181)
Fixes #168 app/layout.tsx hardcoded an English language attribute on the only root html element in the app, and app/[lang]/layout.tsx never overrode it, so all six non-default locales served localized body text while declaring themselves English — contradicting the hreflang clusters and per-locale canonicals the site already emits, and making screen readers pick an English voice for Chinese and Japanese pages. A layout only receives params for its own segment and above, so the root layout can never see lang. next/root-params stops at the first layout it meets, and headers() would opt all 740 prerendered pages out of static generation to decide one attribute. So the document shell moves down into app/[lang]/layout.tsx and the root layout becomes a pass-through owning only the locale-independent concerns. documentLanguage() falls back to the default locale for an unsupported segment: middleware's matcher skips paths containing a dot, so /foo.bar/privacy reaches the layout with a non-locale value and answers 200. Ablation confirms that without the guard an arbitrary URL segment becomes the declared language. app/not-found.tsx restores the root element for unmatched URLs, matched to what main rendered rather than redesigned. Static generation preserved at 740 pages. zh-Hans passes through verbatim and is not rewritten to zh-CN.
1 parent 9c29df6 commit 8feb90d

3 files changed

Lines changed: 143 additions & 14 deletions

File tree

‎apps/docs/app/[lang]/layout.tsx‎

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ const LANGUAGE_NAMES: Record<string, string> = {
1313
ko: '한국어',
1414
};
1515

16+
/**
17+
* The value the document declares as its language.
18+
*
19+
* The locale codes in `lib/i18n.ts` are already BCP 47 tags, so a supported
20+
* locale is used verbatim — `zh-Hans` in particular is the correct tag and is
21+
* deliberately not rewritten to `zh-CN`.
22+
*
23+
* The `[lang]` segment is dynamic, so it is not guaranteed to hold one of them.
24+
* `middleware.ts` normalises every path it sees, but its matcher skips paths
25+
* containing a dot, so `/foo.bar/privacy` reaches this layout with `lang` set to
26+
* a string that is not a locale — and that route answers 200, it does not 404.
27+
* An arbitrary URL segment must not become the document's declared language, so
28+
* unsupported segments fall back to the default locale, which is the locale
29+
* `middleware.ts` would have chosen for that request anyway.
30+
*/
31+
function documentLanguage(lang: string): string {
32+
return (i18n.languages as readonly string[]).includes(lang)
33+
? lang
34+
: i18n.defaultLanguage;
35+
}
36+
1637
export default async function LanguageLayout({
1738
params,
1839
children,
@@ -23,15 +44,19 @@ export default async function LanguageLayout({
2344
const { lang } = await params;
2445

2546
return (
26-
<DocsRootProvider
27-
locale={lang}
28-
locales={i18n.languages.map((l) => ({
29-
name: LANGUAGE_NAMES[l] || l,
30-
locale: l,
31-
}))}
32-
>
33-
{children}
34-
</DocsRootProvider>
47+
<html lang={documentLanguage(lang)} suppressHydrationWarning>
48+
<body className="flex flex-col min-h-screen">
49+
<DocsRootProvider
50+
locale={lang}
51+
locales={i18n.languages.map((l) => ({
52+
name: LANGUAGE_NAMES[l] || l,
53+
locale: l,
54+
}))}
55+
>
56+
{children}
57+
</DocsRootProvider>
58+
</body>
59+
</html>
3560
);
3661
}
3762

‎apps/docs/app/layout.tsx‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,34 @@ export const metadata: Metadata = {
1414
},
1515
};
1616

17+
/**
18+
* Pass-through root layout — the document shell lives in `app/[lang]/layout.tsx`.
19+
*
20+
* The document's language attribute has to be the locale, and the locale only
21+
* exists as the `[lang]` route segment *below* this file. A layout only ever
22+
* receives params for its own segment and the ones above it, so this layout
23+
* cannot see `lang`, and neither of the two ways of reaching around that works
24+
* here:
25+
*
26+
* - `next/root-params` collects params only down to the first layout it meets
27+
* walking from the top of the tree (`getRootParamsImpl`). While this file
28+
* exists, that first layout is this one, so `lang` is an ordinary route param
29+
* and never a root param.
30+
* - Reading a middleware-set request header via `headers()` would see it, but
31+
* `headers()` is a dynamic API: it would opt every docs page out of static
32+
* generation to decide one attribute.
33+
*
34+
* So the shell moves down to the segment that owns the locale instead. Every
35+
* user-facing route is nested under `[lang]` (`hideLocale: 'default-locale'`
36+
* only hides the prefix from the URL — the internal route still carries it, see
37+
* `middleware.ts`), so each rendered document still has exactly one root
38+
* element, now declaring the language it is actually written in.
39+
*
40+
* This layout stays because the app has root-level entry points that are not
41+
* under `[lang]` — `app/page.tsx`, plus the metadata and route handlers — and
42+
* it keeps owning what is genuinely locale-independent: the global stylesheet
43+
* and the default metadata, both inherited by every route below.
44+
*/
1745
export default function RootLayout({ children }: { children: ReactNode }) {
18-
return (
19-
<html lang="en" suppressHydrationWarning>
20-
<body className="flex flex-col min-h-screen">{children}</body>
21-
</html>
22-
);
46+
return children;
2347
}

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* 404 shell for routes that are not under the `[lang]` segment.
3+
*
4+
* The document shell lives in `app/[lang]/layout.tsx` so that the language
5+
* attribute can be derived from the locale route segment (see the comment in
6+
* `app/layout.tsx`). That leaves the root layout a pass-through, and this is the
7+
* one route reaching it that renders HTML rather than a redirect or a route
8+
* handler — so without a shell here a 404 would carry no root element at all.
9+
*
10+
* Two paths land on this file, and neither of them renders the `[lang]` layout:
11+
*
12+
* - a path that matches no route (`/no-such-page`), which Next serves from the
13+
* root `_not-found` entry;
14+
* - `notFound()` thrown by a page below `[lang]` (`/docs/no-such-page`), which
15+
* unwinds to the nearest not-found boundary. That boundary is this one, and it
16+
* sits above the locale layout, so that layout's shell is replaced rather than
17+
* wrapped. There is no nesting risk and exactly one root element either way.
18+
*
19+
* `en` is hardcoded deliberately: this boundary is above the locale segment and
20+
* cannot read it, which is the same position the root layout was in before. That
21+
* is what `main` declared here, and matching it keeps the 404 unchanged by the
22+
* locale work rather than quietly redesigned by it. The markup below reproduces
23+
* the Next.js built-in 404 that `main` rendered through the old root layout.
24+
*/
25+
export default function NotFound() {
26+
return (
27+
<html lang="en" suppressHydrationWarning>
28+
<body className="flex flex-col min-h-screen">
29+
<title>404: This page could not be found.</title>
30+
<div
31+
style={{
32+
fontFamily:
33+
'system-ui,"Segoe UI",Roboto,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji"',
34+
height: '100vh',
35+
textAlign: 'center',
36+
display: 'flex',
37+
flexDirection: 'column',
38+
alignItems: 'center',
39+
justifyContent: 'center',
40+
}}
41+
>
42+
<div>
43+
<style
44+
dangerouslySetInnerHTML={{
45+
__html:
46+
'body{color:#000;background:#fff;margin:0}.next-error-h1{border-right:1px solid rgba(0,0,0,.3)}@media (prefers-color-scheme:dark){body{color:#fff;background:#000}.next-error-h1{border-right:1px solid rgba(255,255,255,.3)}}',
47+
}}
48+
/>
49+
<h1
50+
className="next-error-h1"
51+
style={{
52+
display: 'inline-block',
53+
margin: '0 20px 0 0',
54+
padding: '0 23px 0 0',
55+
fontSize: 24,
56+
fontWeight: 500,
57+
verticalAlign: 'top',
58+
lineHeight: '49px',
59+
}}
60+
>
61+
404
62+
</h1>
63+
<div style={{ display: 'inline-block' }}>
64+
<h2
65+
style={{
66+
fontSize: 14,
67+
fontWeight: 400,
68+
lineHeight: '49px',
69+
margin: 0,
70+
}}
71+
>
72+
This page could not be found.
73+
</h2>
74+
</div>
75+
</div>
76+
</div>
77+
</body>
78+
</html>
79+
);
80+
}

0 commit comments

Comments
 (0)