Skip to content

Commit 16a036f

Browse files
committed
Use Inter font when generating icons to prevent ArrayBuffer re-use. (#3100)
1 parent f07982d commit 16a036f

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

packages/gitbook/src/app/middleware/(site)/(core)/~gitbook/icon/route.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,15 @@ export const runtime = 'edge';
88

99
export async function GET(req: NextRequest) {
1010
try {
11-
console.log('icon: getSiteContentPointer');
1211
const pointer = await getSiteContentPointer();
13-
console.log('icon: fetchV1context');
1412
const context = await fetchV1ContextForSitePointer(pointer);
1513

1614
// biome-ignore lint/suspicious/noConsole: we want to log here
1715
console.log(`serving icon for ${context.site.id}`);
18-
1916
return await serveIcon(context, req);
2017
} catch (err) {
21-
if (err instanceof Error) {
22-
console.error(`icon: ${err.toString()}`, err.stack);
23-
}
24-
18+
// biome-ignore lint/suspicious/noConsole: we want to log here
19+
console.log(`serveIcon error: ${err}`, (err as Error).stack);
2520
throw err;
2621
}
2722
}

packages/gitbook/src/lib/v1.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ async function getDataFetcherV1(): Promise<GitBookDataFetcher> {
285285
export async function fetchV1ContextForSitePointer(pointer: SiteContentPointer) {
286286
const baseContext = await getV1BaseContext();
287287

288-
console.log('icon: baseContext success');
289288
const context = await fetchSiteContextByIds(baseContext, {
290289
organization: pointer.organizationId,
291290
site: pointer.siteId,
@@ -296,10 +295,8 @@ export async function fetchV1ContextForSitePointer(pointer: SiteContentPointer)
296295
changeRequest: pointer.changeRequestId,
297296
revision: pointer.revisionId,
298297
});
299-
console.log('icon: context inner success');
300298

301299
context.customization = await getDynamicCustomizationSettings(context.customization);
302-
console.log('icon: customization success');
303300

304301
return context;
305302
}

packages/gitbook/src/routes/icon.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ const SIZES = {
2727
* Generate an icon for a site content.
2828
*/
2929
export async function serveIcon(context: GitBookSiteContext, req: Request) {
30-
console.log('icon: serveIcon', req.url);
3130
const options = getOptions(req.url);
3231
const size = SIZES[options.size];
3332

3433
const { site, customization } = context;
3534
const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null;
3635

36+
console.log(
37+
'icon: serveIcon',
38+
req.url,
39+
customIcon ? 'custom' : 'emoji' in customization.favicon ? 'emoji' : 'fallback'
40+
);
3741
// If the site has a custom icon, redirect to it
3842
if (customIcon) {
3943
const iconUrl = options.theme === 'light' ? customIcon.light : customIcon.dark;
@@ -47,6 +51,17 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {
4751

4852
const contentTitle = site.title;
4953

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+
5065
return new ImageResponse(
5166
<div
5267
tw={tcls(options.theme === 'light' ? 'bg-white' : 'bg-black', size.boxStyle)}
@@ -56,6 +71,7 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {
5671
display: 'flex',
5772
alignItems: 'center',
5873
justifyContent: 'center',
74+
fontFamily: 'Inter',
5975
}}
6076
>
6177
<h2
@@ -74,6 +90,14 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {
7490
{
7591
width: size.width,
7692
height: size.height,
93+
fonts: [
94+
{
95+
data: dst,
96+
name: 'Inter',
97+
weight: 400,
98+
style: 'normal',
99+
},
100+
],
77101
}
78102
);
79103
}

0 commit comments

Comments
 (0)