|
1 | 1 | /** |
2 | | - * Default OG image — bypass HTMLRewriter by using React elements directly. |
3 | | - * workers-og's og() accepts React.ReactNode, skipping parseHtml entirely. |
| 2 | + * Default OG image — isolate emoji vs spans in logo row. |
4 | 3 | */ |
5 | 4 | import type { APIRoute } from "astro"; |
6 | 5 | import { ImageResponse } from "workers-og"; |
7 | 6 | import { loadFonts } from "../../../lib/og-utils"; |
8 | 7 |
|
9 | 8 | export const prerender = false; |
10 | 9 |
|
| 10 | +const base = (inner: string) => |
| 11 | + `<div style="display:flex;width:1200px;height:630px;background:#000;padding:60px"><div style="display:flex;flex-direction:column;width:100%;height:100%;justify-content:space-between">${inner}<div style="display:flex;color:#fff;font-size:48px">Title</div><div style="display:flex;color:#888;font-size:16px">Footer</div></div></div>`; |
| 12 | + |
11 | 13 | export const GET: APIRoute = async ({ url }) => { |
| 14 | + const variant = url.searchParams.get("v") || "1"; |
| 15 | + |
| 16 | + const variants: Record<string, string> = { |
| 17 | + // 1: Just text, no emoji, no spans |
| 18 | + "1": base('<div style="display:flex;align-items:center;gap:12px"><div style="color:#fff;font-size:24px">CodingCat.dev</div></div>'), |
| 19 | + // 2: Emoji only (is HTMLRewriter chunking the emoji?) |
| 20 | + "2": base('<div style="display:flex;align-items:center;gap:12px"><div style="font-size:40px">🐱</div><div style="color:#fff;font-size:24px">CodingCat.dev</div></div>'), |
| 21 | + // 3: Spans only, no emoji |
| 22 | + "3": base('<div style="display:flex;align-items:center;gap:12px"><div style="display:flex;font-size:24px;color:#fff"><span>CodingCat</span><span style="color:#7c3aed">.dev</span></div></div>'), |
| 23 | + // 4: Emoji + spans (full logo) |
| 24 | + "4": base('<div style="display:flex;align-items:center;gap:12px"><div style="font-size:40px">🐱</div><div style="display:flex;font-size:24px;font-weight:700;color:#fff"><span>CodingCat</span><span style="color:#7c3aed">.dev</span></div></div>'), |
| 25 | + // 5: ASCII cat instead of emoji |
| 26 | + "5": base('<div style="display:flex;align-items:center;gap:12px"><div style="font-size:40px">CC</div><div style="display:flex;font-size:24px;font-weight:700;color:#fff"><span>CodingCat</span><span style="color:#7c3aed">.dev</span></div></div>'), |
| 27 | + }; |
| 28 | + |
| 29 | + const html = variants[variant] || variants["1"]; |
| 30 | + |
| 31 | + if (url.searchParams.get("mode") === "dump") { |
| 32 | + return new Response(html, { headers: { "Content-Type": "text/plain" } }); |
| 33 | + } |
| 34 | + |
12 | 35 | try { |
13 | | - // Pass a React element directly — bypasses HTMLRewriter parseHtml |
14 | | - const element = { |
15 | | - type: "div", |
16 | | - props: { |
17 | | - style: { |
18 | | - display: "flex", |
19 | | - width: 1200, |
20 | | - height: 630, |
21 | | - background: "#000", |
22 | | - color: "#fff", |
23 | | - fontSize: 48, |
24 | | - alignItems: "center", |
25 | | - justifyContent: "center", |
26 | | - }, |
27 | | - children: "Hello World", |
28 | | - }, |
29 | | - }; |
30 | | - |
31 | | - const response = new ImageResponse(element as any, { |
| 36 | + const response = new ImageResponse(html, { |
32 | 37 | width: 1200, |
33 | 38 | height: 630, |
34 | 39 | fonts: loadFonts(), |
35 | 40 | }); |
36 | 41 |
|
37 | 42 | const buffer = await response.arrayBuffer(); |
38 | 43 | if (buffer.byteLength === 0) { |
39 | | - return new Response(JSON.stringify({ error: "Empty buffer" }), { |
| 44 | + return new Response(JSON.stringify({ error: "Empty buffer", variant }), { |
40 | 45 | status: 500, headers: { "Content-Type": "application/json" }, |
41 | 46 | }); |
42 | 47 | } |
43 | 48 |
|
44 | 49 | return new Response(buffer, { |
45 | | - headers: { |
46 | | - "Content-Type": "image/png", |
47 | | - "Content-Length": buffer.byteLength.toString(), |
48 | | - }, |
| 50 | + headers: { "Content-Type": "image/png", "Content-Length": buffer.byteLength.toString() }, |
49 | 51 | }); |
50 | 52 | } catch (e: any) { |
51 | | - return new Response(JSON.stringify({ error: e.message, stack: e.stack }), { |
| 53 | + return new Response(JSON.stringify({ error: e.message, variant }), { |
52 | 54 | status: 500, headers: { "Content-Type": "application/json" }, |
53 | 55 | }); |
54 | 56 | } |
|
0 commit comments