返回 CodeWhale
opengraph-image.tsx
根目录 / web / app / opengraph-image.tsx
1 import { readFile } from "node:fs/promises";
2 import { join } from "node:path";
3 import { ImageResponse } from "next/og";
4 import { IDENTITY_PHRASE, OG_ALT } from "@/lib/page-meta";
5
6 export const alt = OG_ALT;
7 export const size = { width: 1200, height: 630 };
8 export const contentType = "image/png";
9
10 // Read the brand SVGs when the image is generated, never at import time. This
11 // route is prerendered at build on Node, but its module is still evaluated
12 // inside the Cloudflare Worker whenever a page regenerates, and the Workers
13 // Node shim throws on fs.readFile. A top-level read here made every ISR
14 // regeneration on the site fail with a 500 and serve its build snapshot forever.
15 async function brandSvgs(): Promise<[string, string]> {
16 const [mark, wordmark] = await Promise.all([
17 readFile(join(process.cwd(), "public/brand/mark-reversed.svg")),
18 readFile(join(process.cwd(), "public/brand/wordmark-inverted.svg")),
19 ]);
20 return [mark.toString(), wordmark.toString()];
21 }
22
23 export default async function OpengraphImage() {
24 const [markSvg, wordmarkSvg] = await brandSvgs();
25 const markDataUrl = `data:image/svg+xml;base64,${Buffer.from(markSvg).toString("base64")}`;
26 const wordmarkDataUrl = `data:image/svg+xml;base64,${Buffer.from(wordmarkSvg).toString("base64")}`;
27
28 return new ImageResponse(
29 (
30 <div
31 style={{
32 width: "100%",
33 height: "100%",
34 display: "flex",
35 flexDirection: "column",
36 alignItems: "center",
37 justifyContent: "center",
38 gap: 34,
39 backgroundImage: "linear-gradient(180deg, #142352 0%, #142638 55%, #0b48bb 100%)",
40 fontFamily: "sans-serif",
41 }}
42 >
43 <img src={markDataUrl} width={240} height={240} alt="" />
44 {/* The family wordmark is 1024x160 (6.4:1). */}
45 <img src={wordmarkDataUrl} width={532} height={83} alt="Codewhale" />
46 <div style={{ display: "flex", fontSize: 30, color: "#e6eef7", marginTop: 14 }}>
47 {IDENTITY_PHRASE}
48 </div>
49 </div>
50 ),
51 { ...size },
52 );
53 }
54
54 lines Plain Text