Skip to content

Commit cb5e463

Browse files
committed
🐛 fix OG images for non-English locales
Move OG route from /og/docs/[...slug] to /og/[lang]/docs/[...slug] so locale-prefixed OG URLs resolve correctly. Pass lang to source.getPage and strip locale/docs prefix from segments for generateStaticParams. https://claude.ai/code/session_01UwMuBPaeouJ16zJTVa8UJm
1 parent f86c84b commit cb5e463

3 files changed

Lines changed: 41 additions & 30 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { getPageImage, source } from "@/lib/source";
2+
import { notFound } from "next/navigation";
3+
import { ImageResponse } from "next/og";
4+
import { generate as DefaultImage } from "fumadocs-ui/og";
5+
6+
export const revalidate = false;
7+
8+
export async function GET(
9+
_req: Request,
10+
{ params }: { params: Promise<{ lang: string; slug: string[] }> },
11+
) {
12+
const { lang, slug } = await params;
13+
const page = source.getPage(slug.slice(0, -1), lang);
14+
if (!page) notFound();
15+
16+
return new ImageResponse(
17+
<DefaultImage
18+
title={page.data.title}
19+
description={page.data.description}
20+
site="Python Template"
21+
/>,
22+
{
23+
width: 1200,
24+
height: 630,
25+
},
26+
);
27+
}
28+
29+
export function generateStaticParams() {
30+
return source.getPages().map((page) => ({
31+
lang: page.locale,
32+
slug: getPageImage(page).segments,
33+
}));
34+
}

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

Lines changed: 0 additions & 27 deletions
This file was deleted.

docs/lib/source.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ export const source = loader({
99
});
1010

1111
export function getPageImage(page: ReturnType<typeof source.getPage> & {}) {
12-
const segments = page.url.split("/").filter(Boolean);
12+
const allSegments = page.url.split("/").filter(Boolean);
13+
// Strip locale and "docs" prefix for the slug param (they're separate route params)
14+
const docSegments = allSegments.filter(
15+
(s) => s !== page.locale && s !== "docs",
16+
);
1317
return {
14-
url: `/og/${segments.join("/")}/og.png`,
15-
segments: [...segments, "og.png"],
18+
url: `/og/${allSegments.join("/")}/og.png`,
19+
segments: [...docSegments, "og.png"],
1620
};
1721
}
1822

0 commit comments

Comments
 (0)