From 17ae2a4c87ddaeb5709520d65d7b7940cf45a5f7 Mon Sep 17 00:00:00 2001 From: Cameron Otsuka Date: Wed, 17 Dec 2025 13:00:24 -0800 Subject: [PATCH] fallback to dejavu mono if character not available --- src/utils/generateOpenGraphImage.ts | 45 ++++++++++++++++++----------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/src/utils/generateOpenGraphImage.ts b/src/utils/generateOpenGraphImage.ts index a4763b2..59820a5 100644 --- a/src/utils/generateOpenGraphImage.ts +++ b/src/utils/generateOpenGraphImage.ts @@ -1,4 +1,5 @@ import { ImageResponse } from '@vercel/og'; +import { type SatoriOptions } from 'satori'; import { siteAuthor } from '@utils/globals'; async function loadFont(fontName: string) { @@ -10,6 +11,9 @@ async function loadFont(fontName: string) { case 'Source Serif 4': url = `https://cdn.jsdelivr.net/fontsource/fonts/source-serif-4@latest/latin-400-normal.ttf`; break; + case 'DejaVu Mono': + url = `https://cdn.jsdelivr.net/fontsource/fonts/dejavu-mono@latest/latin-400-normal.ttf`; + break; default: throw new Error('font url not defined'); } @@ -21,6 +25,28 @@ async function loadFont(fontName: string) { throw new Error('failed to load font data'); } +const satoriOptions: SatoriOptions = { + width: 1200, + height: 630, + fonts: [ + { + name: 'Public Sans Variable', + data: await loadFont('Public Sans Variable'), + style: 'normal' + }, + { + name: 'Source Serif 4', + data: await loadFont('Source Serif 4'), + style: 'normal' + }, + { + name: 'DejaVu Mono', + data: await loadFont('DejaVu Mono'), + style: 'normal' + } + ] +} + export default async function generateOpenGraphImage(title: string, description: string) { const element = { type: 'div', @@ -55,7 +81,7 @@ export default async function generateOpenGraphImage(title: string, description: style: { fontSize: 32, marginBottom: 40, - fontFamily: 'Source Serif 4' + fontFamily: 'Source Serif 4, DejaVu Mono' }, children: description, }, @@ -96,20 +122,5 @@ export default async function generateOpenGraphImage(title: string, description: key: null } - return new ImageResponse(element, { - width: 1200, - height: 630, - fonts: [ - { - name: 'Public Sans Variable', - data: await loadFont('Public Sans Variable'), - style: 'normal' - }, - { - name: 'Source Serif 4', - data: await loadFont('Source Serif 4'), - style: 'normal' - } - ] - }); + return new ImageResponse(element, satoriOptions); } \ No newline at end of file -- 2.52.0