Skip to content

Commit b078736

Browse files
authored
fix(docs): render the share card in the language of the page it belongs to (#190)
Fixes #185 The generated share card was locale-independent in two compounding places — getPageImage built its URL from page.slugs alone, and the OG route resolved the page with no language argument — so all seven locales served one English card. A Japanese reader sharing a Japanese page got an English preview. getPageImage(page, lang) now carries the locale, and the route passes that language to the loader. lang is required rather than optional-with-a-default: the defect being fixed is a call site that did not pass a language, and a default is what lets the next one reproduce it silently. The locale is spelled out for English too, because an omitted "en" would make /og/docs/es/... ambiguous between the Spanish card and the English card for a page slugged "es". Static params enumerate over translatedLocales() from the English page set, so a page that exists only in English gets exactly one card: 335 cards, matching an independent count off the content tree, not a naive 553. On an untranslated locale route the card URL follows the #188 fallback rule and names the English card — the body being served is the English page, so the card depicting it is the English card. That also makes contentLang a member of translatedLocales() by construction, so no emitted card URL can fall outside the enumeration, which is what makes dynamicParams = false safe. Build cost: 1.15x wall-clock (50.5s to 58.0s) for 4.24x the image count.
1 parent d394550 commit b078736

3 files changed

Lines changed: 108 additions & 11 deletions

File tree

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

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,25 @@ function seoTitleOf(page: DocsPageData): string {
4040
return page.data.seoTitle ?? page.data.title;
4141
}
4242

43-
/** Absolute URL of the generated 1200x630 share card for a page. */
44-
function shareCardUrl(page: DocsPageData): string {
45-
return `${SITE_URL}${getPageImage(page).url}`;
43+
/**
44+
* Absolute URL of the generated 1200x630 share card for a page, in the language
45+
* the card should be rendered in.
46+
*
47+
* `lang` here is the *content* locale, not `params.lang` — the same resolution
48+
* `canonical` and `og:url` already use. On a route whose locale has no
49+
* translation the body being served is the English page, so the card that
50+
* depicts it is the English card, and asking for a `ja` card would produce a
51+
* second URL rendering identical English pixels. That is worse than wasteful:
52+
* `og:image` sits beside an `og:url` naming the English page, so a card URL
53+
* claiming to be the Japanese one would contradict the object it illustrates.
54+
*
55+
* This also makes the fallback rule and the cost ceiling the same rule. Cards
56+
* are enumerated over `translatedLocales`, so resolving the URL through
57+
* `canonicalLocale` is what keeps an English-only page pointing at the one card
58+
* that exists for it rather than at six that were never generated.
59+
*/
60+
function shareCardUrl(page: DocsPageData, lang: string): string {
61+
return `${SITE_URL}${getPageImage(page, lang).url}`;
4662
}
4763

4864
/**
@@ -209,7 +225,7 @@ export default async function Page(props: {
209225
title: seoTitleOf(page),
210226
description: page.data.description,
211227
url: localeUrl(contentLang, docsPath(page.slugs)),
212-
image: shareCardUrl(page),
228+
image: shareCardUrl(page, contentLang),
213229
}),
214230
];
215231

@@ -273,7 +289,10 @@ export async function generateMetadata(props: {
273289
const canonical = localeUrl(contentLang, path);
274290
const title = seoTitleOf(page);
275291
const description = page.data.description;
276-
const image = shareCardUrl(page);
292+
// Rendered in `contentLang`, matching `title`/`description` above: the loader
293+
// already resolved those through the same fallback, so the card's text and
294+
// the metadata's text are the same strings in the same language.
295+
const image = shareCardUrl(page, contentLang);
277296

278297
return {
279298
title,

‎apps/docs/app/og/docs/[...slug]/route.tsx‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,58 @@
11
import { SITE_NAME, getPageImage, source } from '@/lib/source';
2+
import { i18n } from '@/lib/i18n';
3+
import { translatedLocales } from '@/lib/seo';
24
import { notFound } from 'next/navigation';
35
import { ImageResponse } from 'next/og';
46
import { generate as DefaultImage } from 'fumadocs-ui/og';
57

68
export const revalidate = false;
79

10+
/**
11+
* Only the cards `generateStaticParams` enumerates are servable.
12+
*
13+
* Without this the route stays willing to render any locale for any page on
14+
* demand, so `/og/docs/ja/operate/backup/image.png` — a page with no Japanese
15+
* translation — would render the English fallback text under a `ja` URL, which
16+
* is the exact incoherence this route is being fixed to stop asserting. The
17+
* prerendered set is not merely the cards we bothered to build ahead of time;
18+
* it is the set of cards that exist.
19+
*
20+
* Safe because both sides of the contract derive their locale the same way. A
21+
* page's metadata asks for the card at its `contentLang`, and `contentLang` is
22+
* a member of `translatedLocales(slugs)` by construction — `canonicalLocale`
23+
* returns either a locale already in that list or the default language, which
24+
* the list always contains for a page that has an English source. So no card
25+
* URL this site emits can fall outside the enumeration below.
26+
*/
27+
export const dynamicParams = false;
28+
29+
/** Locale tags, as the narrow union `i18n.languages` is actually typed with. */
30+
type Locale = (typeof i18n.languages)[number];
31+
32+
/**
33+
* Whether a raw first path segment names a supported locale.
34+
*
35+
* A predicate rather than a bare `.includes` so the check also narrows: past
36+
* it, `lang` is a `Locale` and the loader is being handed a language it has
37+
* declared, not an arbitrary string that happened to arrive in a URL.
38+
*/
39+
function isLocale(value: string): value is Locale {
40+
return (i18n.languages as readonly string[]).includes(value);
41+
}
42+
843
export async function GET(
944
_req: Request,
1045
{ params }: { params: Promise<{ slug: string[] }> },
1146
) {
1247
const { slug } = await params;
13-
const page = source.getPage(slug.slice(0, -1));
48+
// `[lang, ...page slugs, 'image.png']` — the shape `getPageImage` builds.
49+
const [lang, ...rest] = slug;
50+
if (!isLocale(lang)) notFound();
51+
52+
// The language argument whose absence was the defect: without it the loader
53+
// resolves the default-language page and every locale's card renders the
54+
// English title and description.
55+
const page = source.getPage(rest.slice(0, -1), lang);
1456
if (!page) notFound();
1557

1658
return new ImageResponse(
@@ -26,8 +68,20 @@ export async function GET(
2668
);
2769
}
2870

71+
/**
72+
* One card per (page, locale that really translates it).
73+
*
74+
* Enumerated from the English page set because English is the authored source
75+
* — `getPages()` with no argument returns all seven locales' page sets
76+
* concatenated, and `getPages(lang)` cannot tell a real translation from a
77+
* fallback copy, so neither answers "which pages exist". `translatedLocales`
78+
* is the function that can, and using it is what keeps a page that exists only
79+
* in English at exactly one card instead of seven.
80+
*/
2981
export function generateStaticParams() {
30-
return source.getPages().map((page) => ({
31-
slug: getPageImage(page).segments,
32-
}));
82+
return source.getPages(i18n.defaultLanguage).flatMap((page) =>
83+
translatedLocales(page.slugs).map((lang) => ({
84+
slug: getPageImage(page, lang).segments,
85+
})),
86+
);
3387
}

‎apps/docs/lib/source.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,32 @@ export const source = loader({
2424
*/
2525
export const SITE_NAME = 'ObjectOS';
2626

27-
export function getPageImage(page: InferPageType<typeof source>) {
28-
const segments = [...page.slugs, 'image.png'];
27+
/**
28+
* The generated share card for `page`, as seen from `lang`.
29+
*
30+
* The locale is a path segment because the card is a rendered artifact of the
31+
* page's text, not a decoration attached to it: a Japanese page and its English
32+
* source produce two different 1200x630 images, so they need two URLs. Building
33+
* the segments from `page.slugs` alone gave all seven locales one URL, and
34+
* whichever card was generated first was the one every locale served.
35+
*
36+
* `lang` is spelled out for every locale, English included, rather than hidden
37+
* the way `localeUrl` hides the default language from reader-facing URLs. Two
38+
* reasons, both about the route rather than the reader: `hideLocale` exists so
39+
* `/docs/x` reads as the plain English URL, and nothing reads an image asset
40+
* path; and an omitted `en` would make the first segment ambiguous — the route
41+
* handler would have to guess whether `/og/docs/es/...` means the Spanish card
42+
* for `…/…` or the English card for a page slugged `es`. That guess is wrong
43+
* the day someone adds `content/docs/es.mdx`, and it fails silently.
44+
*
45+
* `lang` is required rather than optional-with-a-default for the same reason
46+
* `languageAlternates` requires its `locales`: the defect being fixed here is a
47+
* call site that did not pass a language, and a default value is exactly what
48+
* lets the next call site reproduce it without anyone noticing. Required makes
49+
* `tsc` the thing that notices.
50+
*/
51+
export function getPageImage(page: InferPageType<typeof source>, lang: string) {
52+
const segments = [lang, ...page.slugs, 'image.png'];
2953

3054
return {
3155
segments,

0 commit comments

Comments
 (0)