1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2026-06-05 19:35:27 -04:00
Files
jarv.is/lib/og-utils.ts
T
jake 4858c8928c refactor: update font imports and variables
- Replaced GeistSans and GeistMono with Inter and JetBrainsMono in globals.css and layout.tsx.
- Updated font variable names to reflect the new font choices in fonts.ts.
2026-02-19 13:36:13 -05:00

32 lines
987 B
TypeScript

import { cacheLife } from "next/cache";
// Load a Google Font from the Google Fonts API
// Adapted from https://github.com/brianlovin/briOS/blob/f72dc33a11194de45c80337b22be4560da62ad7e/src/lib/og-utils.tsx#L32
export async function loadGoogleFont(font: string, weight: number): Promise<ArrayBuffer> {
"use cache";
const url = `https://fonts.googleapis.com/css2?family=${font}:wght@${weight}`;
const cssResponse = await fetch(url, {
next: {
revalidate: 31_536_000, // 1 year
},
});
const css = await cssResponse.text();
const resource = css.match(/src: url\((.+)\) format\('(opentype|truetype)'\)/);
if (resource) {
const fontResponse = await fetch(resource[1], {
next: {
revalidate: 31_536_000, // 1 year
},
});
if (fontResponse.status === 200) {
cacheLife("max"); // cache indefinitely if successful
return fontResponse.arrayBuffer();
}
}
throw new Error(`Failed to load font: ${font} ${weight}`);
}