Skip to content

Commit ffe36c5

Browse files
authored
fix(docs): reject a non-locale first segment at the route level (#209)
generateStaticParams on app/[lang]/layout.tsx already enumerated the seven locales, but nothing consumed that as a constraint. dynamicParams = false makes it one, so a first segment that is not a locale resolves to the prerendered /_not-found route instead of rendering a page under a bogus locale. Closes the soft-404 family: /foo.bar/privacy and /foo.bar/terms answered 200, /foo.bar and /favicon.ico answered 307 into the docs route. All now serve a document byte-identical to /no-such-page (sha256 fa59d702...), verified against a running build with script bodies stripped. The 500 the card reported was already fixed incidentally by 6653744 and did not reproduce. notFound() in the layout was measured and rejected: it answers 404 with a 594-byte empty error shell and no 404 copy, because these URLs are dynamically rendered by construction. Prerender count 996/996 unchanged; the dotted machine-facing routes (llms.txt, llms-full.txt, sitemap.xml, robots.txt, .mdx) confirmed untouched. Closes #180
1 parent c5e7905 commit ffe36c5

1 file changed

Lines changed: 52 additions & 7 deletions

File tree

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

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ const LANGUAGE_NAMES: Record<string, string> = {
2020
* locale is used verbatim — `zh-Hans` in particular is the correct tag and is
2121
* deliberately not rewritten to `zh-CN`.
2222
*
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.
23+
* `dynamicParams = false` below now means only an enumerated locale ever
24+
* reaches this layout, so the fallback is unreachable in practice. It stays
25+
* because this function has to be total over `string` — `lang` is typed as one
26+
* — and because a language attribute is the wrong place to learn that a
27+
* routing invariant broke. The default locale is what `middleware.ts` would
28+
* have negotiated for such a request anyway, so the fallback is the quiet
29+
* answer rather than a guess.
3030
*/
3131
function documentLanguage(lang: string): string {
3232
return (i18n.languages as readonly string[]).includes(lang)
@@ -60,6 +60,51 @@ export default async function LanguageLayout({
6060
);
6161
}
6262

63+
/**
64+
* Only the locales `generateStaticParams` enumerates are servable under this
65+
* segment; every other first segment is answered by the prerendered
66+
* `/_not-found` route.
67+
*
68+
* `middleware.ts` is supposed to put a locale on every path, but its matcher
69+
* exempts `.*\..*` — unanchored, so it skips any path containing a dot
70+
* anywhere, not just an asset. A request whose *first* segment carries a dot
71+
* therefore reaches these routes with `lang` set to something that is not a
72+
* locale. Measured on the tree before this flag: `/foo.bar/privacy` and
73+
* `/foo.bar/terms` rendered a full page and answered **200**, `/foo.bar`
74+
* answered 307 to `/foo.bar/docs`, and `/favicon.ico` answered 307 to
75+
* `/favicon.ico/docs`. An unbounded family of soft 404s — any dotted first
76+
* segment produces another one — which for a docs site is a crawl-budget and
77+
* duplicate-URL hole.
78+
*
79+
* The fix is here rather than in that matcher on purpose. The dot exemption is
80+
* load-bearing for real **routes**, not only assets: `/llms.txt`,
81+
* `/llms-full.txt`, `/sitemap.xml`, `/robots.txt` and every `.mdx` page route
82+
* (`page.url` plus `.mdx`, which the copy button links to) all contain dots and
83+
* are all exempted by it today. Tightening it would send those through locale
84+
* negotiation, and `check-locale-surface.mjs` reads build output rather than a
85+
* running server, so nothing in CI would report it. Rejecting the segment here
86+
* also fixes strictly more, because it does not care how a bad segment arrived.
87+
*
88+
* **It has to be this flag and not `notFound()` in the layout.** That was
89+
* measured, not assumed: with a `notFound()` guard here instead,
90+
* `/foo.bar/privacy` answers 404 with a 594-byte empty error shell carrying no
91+
* 404 copy at all, because `notFound()` raised while a request is rendered
92+
* *dynamically* escapes both the RSC and the SSR render (#182 established this,
93+
* and these URLs are dynamically rendered by construction — they are in no
94+
* prerendered param set). The status would have looked correct and the body
95+
* would have been blank. `dynamicParams = false` instead removes the dynamic
96+
* render, routing the request to `/_not-found`, an ordinary prerendered page:
97+
* the served document is byte-identical to the one `/no-such-page` has always
98+
* returned.
99+
*
100+
* The behavioural cost, stated plainly: a locale that is not in `i18n.languages`
101+
* needs a rebuild to appear. `lib/i18n.ts` is a checked-in constant and AGENTS.md
102+
* names it the authority for the locale list, so that is already true in
103+
* practice. `app/[lang]/docs/[[...slug]]/page.tsx` and `app/og/docs/[...slug]`
104+
* set the same flag for the same reason.
105+
*/
106+
export const dynamicParams = false;
107+
63108
export async function generateStaticParams() {
64109
return i18n.languages.map((lang) => ({ lang }));
65110
}

0 commit comments

Comments
 (0)