|
1 | 1 | /** |
2 | | - * Default OG image — debug: returns raw HTML to inspect what Satori receives. |
| 2 | + * Default OG image generation for generic pages. |
3 | 3 | */ |
4 | 4 | import type { APIRoute } from "astro"; |
5 | | -import { generateDefaultOgHtml } from "../../../lib/og-utils"; |
| 5 | +import { ImageResponse } from "workers-og"; |
| 6 | +import { generateDefaultOgHtml, loadFonts, OG_CACHE_HEADER } from "../../../lib/og-utils"; |
6 | 7 |
|
7 | 8 | export const prerender = false; |
8 | 9 |
|
9 | 10 | export const GET: APIRoute = async ({ url }) => { |
10 | | - const title = url.searchParams.get("title") || "CodingCat.dev"; |
11 | | - const subtitle = url.searchParams.get("subtitle") || undefined; |
12 | | - const html = generateDefaultOgHtml({ title, subtitle }); |
| 11 | + try { |
| 12 | + const title = url.searchParams.get("title") || "CodingCat.dev"; |
| 13 | + const subtitle = url.searchParams.get("subtitle") || undefined; |
| 14 | + const html = generateDefaultOgHtml({ title, subtitle }); |
13 | 15 |
|
14 | | - return new Response(html, { |
15 | | - headers: { "Content-Type": "text/html" }, |
16 | | - }); |
| 16 | + // Use og() directly via ImageResponse + arrayBuffer() to catch stream errors |
| 17 | + const ogResponse = new ImageResponse(html, { |
| 18 | + width: 1200, |
| 19 | + height: 630, |
| 20 | + fonts: loadFonts(), |
| 21 | + }); |
| 22 | + |
| 23 | + const body = await ogResponse.arrayBuffer(); |
| 24 | + |
| 25 | + if (body.byteLength === 0) { |
| 26 | + return new Response( |
| 27 | + JSON.stringify({ error: "Empty body", htmlLength: html.length }), |
| 28 | + { status: 500, headers: { "Content-Type": "application/json" } } |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + return new Response(body, { |
| 33 | + status: 200, |
| 34 | + headers: { |
| 35 | + "Content-Type": "image/png", |
| 36 | + "Content-Length": body.byteLength.toString(), |
| 37 | + "Cache-Control": OG_CACHE_HEADER, |
| 38 | + }, |
| 39 | + }); |
| 40 | + } catch (error: any) { |
| 41 | + return new Response( |
| 42 | + JSON.stringify({ error: error.message, stack: error.stack }), |
| 43 | + { status: 500, headers: { "Content-Type": "application/json" } } |
| 44 | + ); |
| 45 | + } |
17 | 46 | }; |
0 commit comments