import { ImageResponse } from "next/og"; import { readFile } from "node:fs/promises"; import { join } from "node:path"; export { getPageTitle } from "@/lib/page-titles"; // Cache font data in memory after first load let fontCache: { geistRegular: Buffer; geistPixelSquare: Buffer } | null = null; async function loadFonts() { if (fontCache) return fontCache; const [geistRegular, geistPixelSquare] = await Promise.all([ readFile(join(process.cwd(), "public/Geist-Regular.ttf")), readFile(join(process.cwd(), "public/GeistPixel-Square.ttf")), ]); fontCache = { geistRegular, geistPixelSquare }; return fontCache; } export async function renderOgImage(title: string) { const { geistRegular, geistPixelSquare } = await loadFonts(); return new ImageResponse(
/ json-render
{title.split("\n").map((line, i) => ( {line} ))}
, { width: 1200, height: 630, fonts: [ { name: "Geist", data: geistRegular.buffer as ArrayBuffer, style: "normal", weight: 400, }, { name: "Geist Pixel Square", data: geistPixelSquare.buffer as ArrayBuffer, style: "normal", weight: 500, }, ], }, ); }