Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Keep the Google font in memory between OG images

Rafi6d48353de001
diff --git a/apps/web/app/api/og/load-google-font.ts b/apps/web/app/api/og/load-google-font.ts
index ac710d584a..600eb04616 100644
--- a/apps/web/app/api/og/load-google-font.ts
+++ b/apps/web/app/api/og/load-google-font.ts
@@ -1,4 +1,27 @@
+// a warm instance keeps each font, so that the next image skips the 2 requests
+// to Google Fonts. Satori already keeps the images that it fetches.
+const fontCache = new Map<string, Promise<ArrayBuffer | undefined>>();
+
export async function loadGoogleFont(font: string) {
+ let fontPromise = fontCache.get(font);
+
+ if (!fontPromise) {
+ fontPromise = fetchGoogleFont(font);
+ fontCache.set(font, fontPromise);
+ }
+
+ try {
+ const data = await fontPromise;
+ // try again on the next image if Google Fonts did not return the font
+ if (!data) fontCache.delete(font);
+ return data;
+ } catch (error) {
+ fontCache.delete(font);
+ throw error;
+ }
+}
+
+async function fetchGoogleFont(font: string) {
const url = `https://fonts.googleapis.com/css2?family=${font}`;
const css = await (await fetch(url)).text();
const resource = css.match(