Retro Bulletin Board Systems on atproto. Web app and TUI. lazy mirror of alyraffauf/atbbs atbbs.xyz
forums python tui atproto bbs
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

opengraph: cache og images

+15 -14
+15 -14
opengraph/index.ts
··· 279 279 const url = new URL(request.url); 280 280 const path = url.pathname; 281 281 282 - // Dynamic og:image at /og/bbs/... 282 + // Dynamic og:image at /og/bbs/... — cached at the edge for 1 hour. 283 283 if (path.startsWith("/og/bbs/")) { 284 + const cache = caches.default; 285 + const cachedResponse = await cache.match(request); 286 + if (cachedResponse) return cachedResponse; 287 + 284 288 const route = parseRoute(path); 285 - if (!route) { 286 - return renderOgImage(DEFAULT_TITLE, "", DEFAULT_DESCRIPTION); 287 - } 289 + let imageResponse: Response; 288 290 289 291 try { 290 - const metadata = await fetchMetadata(route); 291 - if (metadata) { 292 - return renderOgImage( 293 - metadata.title, 294 - metadata.subtitle, 295 - metadata.description, 296 - ); 297 - } 292 + const metadata = route ? await fetchMetadata(route) : null; 293 + imageResponse = metadata 294 + ? await renderOgImage(metadata.title, metadata.subtitle, metadata.description) 295 + : await renderOgImage(DEFAULT_TITLE, "", DEFAULT_DESCRIPTION); 298 296 } catch { 299 - // Fall through to default image. 297 + imageResponse = await renderOgImage(DEFAULT_TITLE, "", DEFAULT_DESCRIPTION); 300 298 } 301 299 302 - return renderOgImage(DEFAULT_TITLE, "", DEFAULT_DESCRIPTION); 300 + const cachedCopy = new Response(imageResponse.body, imageResponse); 301 + cachedCopy.headers.set("Cache-Control", "public, max-age=3600"); 302 + await cache.put(request, cachedCopy.clone()); 303 + return cachedCopy; 303 304 } 304 305 305 306 // Inject metadata into HTML for /bbs/... routes.