@@ -27,13 +27,17 @@ const SIZES = {
27
27
* Generate an icon for a site content.
28
28
*/
29
29
export async function serveIcon ( context : GitBookSiteContext , req : Request ) {
30
- console . log ( 'icon: serveIcon' , req . url ) ;
31
30
const options = getOptions ( req . url ) ;
32
31
const size = SIZES [ options . size ] ;
33
32
34
33
const { site, customization } = context ;
35
34
const customIcon = 'icon' in customization . favicon ? customization . favicon . icon : null ;
36
35
36
+ console . log (
37
+ 'icon: serveIcon' ,
38
+ req . url ,
39
+ customIcon ? 'custom' : 'emoji' in customization . favicon ? 'emoji' : 'fallback'
40
+ ) ;
37
41
// If the site has a custom icon, redirect to it
38
42
if ( customIcon ) {
39
43
const iconUrl = options . theme === 'light' ? customIcon . light : customIcon . dark ;
@@ -47,6 +51,17 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {
47
51
48
52
const contentTitle = site . title ;
49
53
54
+ // Load the font locally to prevent the shared instance used by ImageResponse.
55
+ const fontOrigin = await fetch ( new URL ( '../fonts/Inter/Inter-Regular.ttf' , import . meta. url ) ) . then (
56
+ ( res ) => res . arrayBuffer ( )
57
+ ) ;
58
+ const dst = new ArrayBuffer ( fontOrigin . byteLength ) ;
59
+ new Uint8Array ( dst ) . set ( new Uint8Array ( fontOrigin ) ) ;
60
+
61
+ if ( dst . detached ) {
62
+ console . log ( 'about to use detached font buffer..' )
63
+ }
64
+
50
65
return new ImageResponse (
51
66
< div
52
67
tw = { tcls ( options . theme === 'light' ? 'bg-white' : 'bg-black' , size . boxStyle ) }
@@ -56,6 +71,7 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {
56
71
display : 'flex' ,
57
72
alignItems : 'center' ,
58
73
justifyContent : 'center' ,
74
+ fontFamily : 'Inter' ,
59
75
} }
60
76
>
61
77
< h2
@@ -74,6 +90,14 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {
74
90
{
75
91
width : size . width ,
76
92
height : size . height ,
93
+ fonts : [
94
+ {
95
+ data : dst ,
96
+ name : 'Inter' ,
97
+ weight : 400 ,
98
+ style : 'normal' ,
99
+ } ,
100
+ ] ,
77
101
}
78
102
) ;
79
103
}
0 commit comments