The Open Standard for Generative UI
docs: serve OG images with a .png/.jpg extension (#1404)
devin-ai-integration[bot]9116d4a90e22
diff --git a/docs/app/og/[...path]/route.tsx b/docs/app/og/[...path]/route.tsxindex f6077d59e..936d69509 100644--- a/docs/app/og/[...path]/route.tsx+++ b/docs/app/og/[...path]/route.tsx@@ -1,4 +1,5 @@import { getOgImages } from "@/lib/og/entries";+import { ogImagePath } from "@/lib/og/images";import { renderOgCard } from "@/lib/og/render";import { notFound } from "next/navigation";@@ -6,13 +7,18 @@ export const dynamic = "force-static";export const dynamicParams = false;export const revalidate = false;+/** Every OG image keyed by its file path, e.g. `/og/compare.png`. */+function imagesByFile() {+ return new Map([...getOgImages().values()].map((image) => [ogImagePath(image), image]));+}+export function generateStaticParams() {- return [...getOgImages().keys()].map((path) => ({ path: path.slice(1).split("/") }));+ return [...imagesByFile().keys()].map((file) => ({ path: file.slice("/og/".length).split("/") }));}export async function GET(_req: Request, { params }: RouteContext<"/og/[...path]">) {const { path } = await params;- const image = getOgImages().get(`/${path.join("/")}`);+ const image = imagesByFile().get(`/og/${path.join("/")}`);if (!image) notFound();return renderOgCard(image.card);}diff --git a/docs/lib/og/images.ts b/docs/lib/og/images.tsindex 1c4700f57..06ab66e09 100644--- a/docs/lib/og/images.ts+++ b/docs/lib/og/images.ts@@ -22,14 +22,19 @@ function cardAssets(card: OgCardData) {}/**- * `/og/<page path>?v=<hash>`. The hash covers the card content, the artwork bytes, and+ * `/og/<page path>.<png|jpg>?v=<hash>`. The hash covers the card content, the artwork bytes, and* `TEMPLATE_VERSION`, so link-preview caches refetch whenever the rendered image would change.* The route ignores the query string.*/export function ogImageUrl({ path, card }: OgImage) {const hash = createHash("sha256").update(`${TEMPLATE_VERSION}\n${JSON.stringify(card)}`);for (const asset of cardAssets(card)) hash.update(readFileSync(publicPath(asset)));- return `/og${path}?v=${hash.digest("hex").slice(0, 10)}`;+ return `${ogImagePath({ path, card })}?v=${hash.digest("hex").slice(0, 10)}`;+}++/** `/og/<page path>.<png|jpg>`, so the image URL carries a file extension like a static asset. */+export function ogImagePath({ path, card }: OgImage) {+ return `/og${path}.${ogImageFormat(card) === "jpeg" ? "jpg" : "png"}`;}/**