Skip to content

Commit 5602623

Browse files
authored
fix(docs): point an untranslated locale URL at the English page it serves (#188)
Fixes #174 /ja/docs/operate/backup has no Japanese source. fumadocs falls back to the default language and serves the English body, so the URL was a second copy of the English page that declared itself canonical. #169 removed it from the sitemap and every hreflang cluster, but "no longer advertised" is not "no longer indexable" — the in-page language switcher links to it from every translated page. When translatedLocales(slugs) does not contain the current locale, every field asserting where the content lives now names the English URL: alternates.canonical, openGraph.url, TechArticle.url, and each BreadcrumbList item. Breadcrumb items resolve crumb by crumb, because a translated page can sit under an untranslated ancestor. og:locale and TechArticle.inLanguage follow the URL they sit beside — a node naming the English URL while claiming inLanguage ja would assert the English page is Japanese. alternates.languages, both share-card URLs and html lang are untouched. Verified against independent oracles: 218 of 553 docs routes change canonical, equal to the 218 untranslated locale/page pairs counted straight from content/docs/. Across all 577 prerendered documents, 370 differ and 370 is again the oracle's count. Blanking absolute URLs and the two language values makes all 370 byte-identical in rendered markup, so navigation, the visible breadcrumb and body text are unchanged.
1 parent 23a976b commit 5602623

1 file changed

Lines changed: 65 additions & 7 deletions

File tree

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

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

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Tab, Tabs } from 'fumadocs-ui/components/tabs';
1010
import { LLMCopyButton, ViewOptions } from '@/components/ai/page-actions';
1111
import { gitConfig } from '@/lib/layout.shared';
1212
import { SITE_URL, languageAlternates, localeUrl, translatedLocales } from '@/lib/seo';
13+
import { i18n } from '@/lib/i18n';
1314
import type { InferPageType } from 'fumadocs-core/source';
1415

1516
/**
@@ -44,6 +45,37 @@ function shareCardUrl(page: DocsPageData): string {
4445
return `${SITE_URL}${getPageImage(page).url}`;
4546
}
4647

48+
/**
49+
* The locale a page's content actually lives in, as seen from `lang`.
50+
*
51+
* `lang` when that locale really has a translation of `slugs`; the default
52+
* language otherwise, because fumadocs serves the English body under every
53+
* unprefixed locale route (`fallbackLanguage` resolves to `defaultLanguage` —
54+
* see `translatedLocales`). `/ja/docs/operate/backup` has no Japanese source,
55+
* so the content sitting at that URL is the English page and every field that
56+
* names where this content lives has to say so.
57+
*
58+
* `translated` is passed in rather than looked up so a caller that already
59+
* needs the list — `generateMetadata` builds the hreflang cluster from it —
60+
* computes it once.
61+
*/
62+
function canonicalLocale(lang: string, translated: readonly string[]): string {
63+
return translated.includes(lang) ? lang : i18n.defaultLanguage;
64+
}
65+
66+
/**
67+
* Absolute URL of the page at `slugs` as seen from `lang`: its own locale URL
68+
* when it is really translated there, the English URL when it is a fallback.
69+
*
70+
* Resolved per page, not once per request, because translation status is a
71+
* property of the individual page. A translated page can sit under an
72+
* untranslated ancestor and vice versa, so the breadcrumb trail has to ask
73+
* about each crumb separately.
74+
*/
75+
function canonicalUrl(lang: string, slugs: string[]): string {
76+
return localeUrl(canonicalLocale(lang, translatedLocales(slugs)), docsPath(slugs));
77+
}
78+
4779
/**
4880
* Open Graph wants `language_TERRITORY`. Our locale tags carry no territory
4981
* (`en`, `zh-Hans`, `ja`, …), so the honest mapping is the tag with the
@@ -77,6 +109,11 @@ function jsonLdHtml(value: Record<string, unknown>): string {
77109
* prefix of its slugs. Each crumb is named by the page that actually sits at
78110
* that path so the name matches what a reader sees in the sidebar; a folder
79111
* with no index page falls back to its humanized segment.
112+
*
113+
* `item` is each crumb's own canonical URL, resolved crumb by crumb: a
114+
* breadcrumb entry is a claim about where *that* page lives, and pointing it at
115+
* a locale URL that only serves an English fallback is the same untruth the
116+
* page's own canonical would be telling.
80117
*/
81118
function breadcrumbListLd(lang: string, slugs: string[]): Record<string, unknown> {
82119
const trails = [[] as string[], ...slugs.map((_, i) => slugs.slice(0, i + 1))];
@@ -90,7 +127,7 @@ function breadcrumbListLd(lang: string, slugs: string[]): Record<string, unknown
90127
name:
91128
source.getPage(trail, lang)?.data.title ??
92129
humanizeSegment(trail[trail.length - 1] ?? 'docs'),
93-
item: localeUrl(lang, docsPath(trail)),
130+
item: canonicalUrl(lang, trail),
94131
})),
95132
};
96133
}
@@ -131,6 +168,13 @@ export default async function Page(props: {
131168

132169
const MDX = page.data.body;
133170

171+
// The locale this page's content really lives in — `params.lang` for a real
172+
// translation, English for a fumadocs fallback. `url` and `inLanguage` are
173+
// properties of one `TechArticle` node, so they resolve together: a node that
174+
// named the English URL while claiming `inLanguage: "ja"` would assert that
175+
// the English page is Japanese.
176+
const contentLang = canonicalLocale(params.lang, translatedLocales(page.slugs));
177+
134178
// Structured data. Emitted from the page rather than from `generateMetadata`,
135179
// which can only produce meta/link elements — the Metadata API has no channel
136180
// for a JSON-LD script. Google reads `application/ld+json` from either the
@@ -139,10 +183,10 @@ export default async function Page(props: {
139183
const jsonLd = [
140184
breadcrumbListLd(params.lang, page.slugs),
141185
techArticleLd({
142-
lang: params.lang,
186+
lang: contentLang,
143187
title: seoTitleOf(page),
144188
description: page.data.description,
145-
url: localeUrl(params.lang, docsPath(page.slugs)),
189+
url: localeUrl(contentLang, docsPath(page.slugs)),
146190
image: shareCardUrl(page),
147191
}),
148192
];
@@ -199,7 +243,12 @@ export async function generateMetadata(props: {
199243

200244
// Logical path is locale-independent; reconstruct each locale URL from slugs.
201245
const path = docsPath(page.slugs);
202-
const canonical = localeUrl(params.lang, path);
246+
const translated = translatedLocales(page.slugs);
247+
// Where this content lives. An untranslated locale route serves the English
248+
// body (fumadocs falls back), so it points at the English URL instead of
249+
// declaring itself canonical — the fallback copy is not a separate page.
250+
const contentLang = canonicalLocale(params.lang, translated);
251+
const canonical = localeUrl(contentLang, path);
203252
const title = seoTitleOf(page);
204253
const description = page.data.description;
205254
const image = shareCardUrl(page);
@@ -211,11 +260,16 @@ export async function generateMetadata(props: {
211260
canonical,
212261
// Only the locales that really have this page. The cluster is keyed by
213262
// the logical path, not by params.lang, so every page in it advertises
214-
// the same reciprocal set.
215-
languages: languageAlternates(path, translatedLocales(page.slugs)),
263+
// the same reciprocal set. Untouched by the fallback rule above: an
264+
// untranslated locale is already absent from it, which is #169's settled
265+
// shape and stays correct whatever `canonical` points at.
266+
languages: languageAlternates(path, translated),
216267
},
217268
openGraph: {
218269
type: 'article',
270+
// og:url is Open Graph's permanent id for the object, so it tracks
271+
// `canonical` — an object identified by the English URL and by this
272+
// locale's URL at the same time is two objects.
219273
url: canonical,
220274
siteName: SITE_NAME,
221275
// Set explicitly rather than inherited: the root layout's `%s | ObjectOS`
@@ -224,7 +278,11 @@ export async function generateMetadata(props: {
224278
// "Views | ObjectOS — ObjectOS" in a share preview.
225279
title,
226280
description,
227-
locale: ogLocale(params.lang),
281+
// The locale of the object og:url names, which is the English page when
282+
// this route is a fallback. Not the same claim as `<html lang>`: that one
283+
// describes the document being served, chrome included, and #181 settled
284+
// it on the route segment.
285+
locale: ogLocale(contentLang),
228286
images: [{ url: image, width: 1200, height: 630, alt: title }],
229287
},
230288
twitter: {

0 commit comments

Comments
 (0)