@@ -72,8 +72,12 @@ export async function serveOGImage(baseContext: GitBookSiteContext, params: Page
7272
7373 const fonts = (
7474 await Promise . all ( [
75- loadGoogleFont ( { fontFamily, text : regularText , weight : 400 } ) ,
76- loadGoogleFont ( { fontFamily, text : boldText , weight : 700 } ) ,
75+ getWithCache ( `google-font:${ fontFamily } :400` , ( ) =>
76+ loadGoogleFont ( { fontFamily, text : regularText , weight : 400 } )
77+ ) ,
78+ getWithCache ( `google-font:${ fontFamily } :700` , ( ) =>
79+ loadGoogleFont ( { fontFamily, text : boldText , weight : 700 } )
80+ ) ,
7781 ] )
7882 ) . filter ( filterOutNullable ) ;
7983
@@ -338,21 +342,27 @@ async function readImage(response: Response) {
338342 return `data:${ contentType } ;base64,${ base64 } ` ;
339343}
340344
341- const staticImagesCache = new Map < string , string > ( ) ;
345+ // biome-ignore lint/suspicious/noExplicitAny: <explanation>
346+ const staticCache = new Map < string , any > ( ) ;
347+
348+ // Do we need to limit the in-memory cache size? I think given the usage, we should be fine.
349+ async function getWithCache < T > ( key : string , fn : ( ) => Promise < T > ) {
350+ const cached = staticCache . get ( key ) as T ;
351+ if ( cached ) {
352+ return Promise . resolve ( cached ) ;
353+ }
354+
355+ const result = await fn ( ) ;
356+ staticCache . set ( key , result ) ;
357+ return result ;
358+ }
342359
343360/**
344361 * Read a static image and cache it in memory.
345362 */
346363async function readStaticImage ( url : string ) {
347- logOnCloudflareOnly ( `Reading static image: ${ url } , cache size: ${ staticImagesCache . size } ` ) ;
348- const cached = staticImagesCache . get ( url ) ;
349- if ( cached ) {
350- return cached ;
351- }
352-
353- const image = await readSelfImage ( url ) ;
354- staticImagesCache . set ( url , image ) ;
355- return image ;
364+ logOnCloudflareOnly ( `Reading static image: ${ url } , cache size: ${ staticCache . size } ` ) ;
365+ return getWithCache ( `static-image:${ url } ` , ( ) => readSelfImage ( url ) ) ;
356366}
357367
358368/**
0 commit comments