From 9e549e81ebc17b194768db960c68258dce43a54d Mon Sep 17 00:00:00 2001 From: Aman Kumar Bairagi <118182376+amanbairagi30@users.noreply.github.com> Date: Mon, 2 Dec 2024 21:16:42 +0530 Subject: [PATCH] feat: adds dynamic OG image (#1605) --- src/app/api/og/home/route.tsx | 141 ++++++++++++++++++++++++++++++++++ src/config/site-config.ts | 2 +- 2 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 src/app/api/og/home/route.tsx diff --git a/src/app/api/og/home/route.tsx b/src/app/api/og/home/route.tsx new file mode 100644 index 000000000..9f09e4ec3 --- /dev/null +++ b/src/app/api/og/home/route.tsx @@ -0,0 +1,141 @@ +import { ImageResponse } from 'next/og'; + +export const runtime = 'edge'; + +export async function GET() { + async function loadGoogleFont(font: string) { + const url = `https://fonts.googleapis.com/css2?family=${font}:wght@600&display=swap`; + const css = await (await fetch(url)).text(); + const resource = css.match( + /src: url\((.+)\) format\('(opentype|truetype)'\)/, + ); + + if (resource) { + const response = await fetch(resource[1]); + if (response.status == 200) { + return await response.arrayBuffer(); + } + } + + throw new Error('failed to load font data'); + } + + const fontData = await loadGoogleFont('Gabarito'); + + return new ImageResponse( + ( +