Skip to content

Commit 93a366c

Browse files
authored
fix(docs): derive the sitemap and hreflang locale surface from real translations (#176)
Fixes #169 source.getPages() with no argument returns every locale's page set concatenated, not the default-language set, so main emitted 3892 sitemap entries over 574 distinct URLs — 3318 exact duplicates — and advertised all seven locales for pages that exist only in English. New translatedLocales(slugs) in lib/seo.ts discriminates a real translation from fumadocs' English fallback by file identity: page.path keeps the original locale-suffixed filename, while the loader's own getPages(lang)/getLanguages() report 79 pages for all seven locales and cannot tell the two apart. languageAlternates(path, locales) now takes the locale set as a required argument so no future call site can silently fall back to all seven. Sitemap: 3892 entries over 574 URLs -> 346 over 346. Per-locale docs counts now match the files on disk exactly (en 79, zh-Hans 62, ja 39, de 38, es 39, fr 39, ko 39). x-default still points at English, English is still emitted for every page, and i18n.languages, routing and the reading experience are untouched.
1 parent ee74379 commit 93a366c

3 files changed

Lines changed: 91 additions & 20 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { File, Folder, Files } from 'fumadocs-ui/components/files';
99
import { Tab, Tabs } from 'fumadocs-ui/components/tabs';
1010
import { LLMCopyButton, ViewOptions } from '@/components/ai/page-actions';
1111
import { gitConfig } from '@/lib/layout.shared';
12-
import { languageAlternates, localeUrl } from '@/lib/seo';
12+
import { languageAlternates, localeUrl, translatedLocales } from '@/lib/seo';
1313

1414
export default async function Page(props: {
1515
params: Promise<{ lang: string; slug?: string[] }>;
@@ -69,7 +69,10 @@ export async function generateMetadata(props: {
6969
description: page.data.description,
7070
alternates: {
7171
canonical: localeUrl(params.lang, path),
72-
languages: languageAlternates(path),
72+
// Only the locales that really have this page. The cluster is keyed by
73+
// the logical path, not by params.lang, so every page in it advertises
74+
// the same reciprocal set.
75+
languages: languageAlternates(path, translatedLocales(page.slugs)),
7376
},
7477
};
7578
}

‎apps/docs/app/sitemap.ts‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,48 @@
11
import type { MetadataRoute } from 'next';
22
import { source } from '@/lib/source';
33
import { i18n } from '@/lib/i18n';
4-
import { languageAlternates, localeUrl } from '@/lib/seo';
4+
import { languageAlternates, localeUrl, translatedLocales } from '@/lib/seo';
55

66
export const revalidate = false;
77

8+
/**
9+
* Locales in which `privacy` and `terms` are actually written. Those two pages
10+
* carry their copy in a `content` record inside their own route component
11+
* (`app/[lang]/privacy/page.tsx`, `app/[lang]/terms/page.tsx`), which falls back
12+
* to English (`content[lang] ?? content.en`) for anything not listed there —
13+
* the same fallback-shaped defect the docs pages have. Keep this in sync with
14+
* those two records.
15+
*/
16+
const STATIC_PAGE_LOCALES = ['en', 'zh-Hans'];
17+
818
export default function sitemap(): MetadataRoute.Sitemap {
919
const entries: MetadataRoute.Sitemap = [];
1020

1121
// Top-level static pages (paths are locale-independent slugs).
12-
const staticPaths: Array<{ path: string; priority: number }> = [
13-
{ path: '', priority: 1 },
14-
{ path: 'privacy', priority: 0.3 },
15-
{ path: 'terms', priority: 0.3 },
22+
const staticPaths: Array<{ path: string; priority: number; locales: readonly string[] }> = [
23+
// The root exists in every locale: it is a language dispatch page that
24+
// redirects to that locale's /docs.
25+
{ path: '', priority: 1, locales: i18n.languages },
26+
{ path: 'privacy', priority: 0.3, locales: STATIC_PAGE_LOCALES },
27+
{ path: 'terms', priority: 0.3, locales: STATIC_PAGE_LOCALES },
1628
];
1729

18-
// Documentation pages, enumerated from the Fumadocs source. Slugs are the
19-
// same across locales, so we reconstruct each locale URL from page.slugs.
20-
const docPaths = source
21-
.getPages()
22-
.map((page) => ({ path: ['docs', ...page.slugs].join('/'), priority: 0.8 }));
30+
// Documentation pages. `source.getPages()` with no argument returns every
31+
// locale's page set concatenated — the same logical page once per language —
32+
// so it must be pinned to the default language to enumerate pages rather than
33+
// page/locale pairs. Slugs are the same across locales, so we reconstruct
34+
// each locale URL from page.slugs.
35+
const docPaths = source.getPages(i18n.defaultLanguage).map((page) => ({
36+
path: ['docs', ...page.slugs].join('/'),
37+
priority: 0.8,
38+
locales: translatedLocales(page.slugs),
39+
}));
2340

24-
for (const { path, priority } of [...staticPaths, ...docPaths]) {
25-
const languages = languageAlternates(path);
26-
for (const lang of i18n.languages) {
41+
for (const { path, priority, locales } of [...staticPaths, ...docPaths]) {
42+
// One reciprocal hreflang cluster per logical path, listing only the
43+
// locales that really have this page, and shared by every entry in it.
44+
const languages = languageAlternates(path, locales);
45+
for (const lang of locales) {
2746
entries.push({
2847
url: localeUrl(lang, path),
2948
changeFrequency: 'weekly',

‎apps/docs/lib/seo.ts‎

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { i18n } from '@/lib/i18n';
2+
import { source } from '@/lib/source';
23

34
// Canonical production host. Keep in sync with middleware's domain redirect.
45
export const SITE_URL = 'https://docs.objectos.ai';
@@ -16,14 +17,62 @@ export function localeUrl(lang: string, path: string): string {
1617
}
1718

1819
/**
19-
* hreflang alternates map for a logical path: every supported locale plus an
20-
* x-default pointing at the English (canonical) URL. Shape matches the
21-
* `alternates.languages` field of Next.js Metadata and MetadataRoute.Sitemap.
20+
* The locales that have a *real* translated source file for `slugs`.
21+
*
22+
* Why this cannot be read off the loader's own per-language APIs: `defineI18n`
23+
* leaves `fallbackLanguage` unset, which fumadocs resolves to `defaultLanguage`
24+
* — so every non-English locale's in-memory file system is constructed by
25+
* copying the English files in and letting real translations overwrite them.
26+
* The consequences, measured against fumadocs-core 16.8.12 with this repo's
27+
* content:
28+
*
29+
* - `source.getPages(lang)` and `source.getLanguages()` report 79 pages for
30+
* *all seven* locales — they cannot tell a translation from a fallback.
31+
* - `source.getPages()` with no argument returns every locale's page set
32+
* concatenated (553), not the default-language set.
33+
*
34+
* What does survive the copy is the file identity: `page.path` keeps the
35+
* ORIGINAL locale-suffixed filename it was authored under. A real Japanese page
36+
* is `operate/backup.ja.mdx`; an inherited fallback is the English
37+
* `operate/backup.mdx` under a `ja` lookup. Comparing against the English
38+
* page's path is therefore the discriminator, and it needs no filesystem access
39+
* at request time.
40+
*
41+
* English is the authored source (AGENTS.md: "English is the single source of
42+
* truth"), so it is present for every real page — and absent here only for a
43+
* translation-only file, which AGENTS.md forbids and which this function
44+
* correctly reports as having no English URL to advertise.
2245
*/
23-
export function languageAlternates(path: string): Record<string, string> {
46+
export function translatedLocales(slugs: string[]): string[] {
47+
const englishPath = source.getPage(slugs, i18n.defaultLanguage)?.path;
48+
49+
return i18n.languages.filter((lang) => {
50+
const page = source.getPage(slugs, lang);
51+
if (!page) return false;
52+
return lang === i18n.defaultLanguage || page.path !== englishPath;
53+
});
54+
}
55+
56+
/**
57+
* hreflang alternates map for a logical path, over the locales that actually
58+
* have a translation of it, plus an x-default pointing at the English
59+
* (canonical) URL. Shape matches the `alternates.languages` field of Next.js
60+
* Metadata and MetadataRoute.Sitemap.
61+
*
62+
* `locales` is required on purpose. It used to be implicit — every caller got
63+
* all seven — and that is the defect this signature removes; a default value
64+
* would let a future call site silently reproduce it.
65+
*/
66+
export function languageAlternates(
67+
path: string,
68+
locales: Iterable<string>,
69+
): Record<string, string> {
70+
const available = new Set(locales);
2471
const languages: Record<string, string> = {};
72+
// Iterate i18n.languages rather than `locales` so the map order is the
73+
// declared locale order regardless of what the caller passes.
2574
for (const lang of i18n.languages) {
26-
languages[lang] = localeUrl(lang, path);
75+
if (available.has(lang)) languages[lang] = localeUrl(lang, path);
2776
}
2877
languages['x-default'] = localeUrl(i18n.defaultLanguage, path);
2978
return languages;

0 commit comments

Comments
 (0)