From 08678546049fd44040105d054a4bf45bf70ac13f Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Sat, 18 Apr 2026 10:19:00 -0400 Subject: [PATCH] opengraph: cache og images --- opengraph/index.ts | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/opengraph/index.ts b/opengraph/index.ts index d1b36a2..0819afd 100644 --- a/opengraph/index.ts +++ b/opengraph/index.ts @@ -279,27 +279,28 @@ export default { const url = new URL(request.url); const path = url.pathname; - // Dynamic og:image at /og/bbs/... + // Dynamic og:image at /og/bbs/... — cached at the edge for 1 hour. if (path.startsWith("/og/bbs/")) { + const cache = caches.default; + const cachedResponse = await cache.match(request); + if (cachedResponse) return cachedResponse; + const route = parseRoute(path); - if (!route) { - return renderOgImage(DEFAULT_TITLE, "", DEFAULT_DESCRIPTION); - } + let imageResponse: Response; try { - const metadata = await fetchMetadata(route); - if (metadata) { - return renderOgImage( - metadata.title, - metadata.subtitle, - metadata.description, - ); - } + const metadata = route ? await fetchMetadata(route) : null; + imageResponse = metadata + ? await renderOgImage(metadata.title, metadata.subtitle, metadata.description) + : await renderOgImage(DEFAULT_TITLE, "", DEFAULT_DESCRIPTION); } catch { - // Fall through to default image. + imageResponse = await renderOgImage(DEFAULT_TITLE, "", DEFAULT_DESCRIPTION); } - return renderOgImage(DEFAULT_TITLE, "", DEFAULT_DESCRIPTION); + const cachedCopy = new Response(imageResponse.body, imageResponse); + cachedCopy.headers.set("Cache-Control", "public, max-age=3600"); + await cache.put(request, cachedCopy.clone()); + return cachedCopy; } // Inject metadata into HTML for /bbs/... routes. -- 2.51.2