Skip to content

Commit 3967d6b

Browse files
committed
Use Inter font when generating icons to prevent ArrayBuffer re-use.
1 parent f07982d commit 3967d6b

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@ 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-
19-
return await serveIcon(context, req);
16+
const response = await serveIcon(context, req);
17+
// biome-ignore lint/suspicious/noConsole: we want to log here
18+
console.log(`served icon for ${context.site.id}`);
19+
return response;
2020
} catch (err) {
21-
if (err instanceof Error) {
22-
console.error(`icon: ${err.toString()}`, err.stack);
23-
}
24-
21+
// biome-ignore lint/suspicious/noConsole: we want to log here
22+
console.log(`serveIcon error: ${err}`, (err as Error).stack);
2523
throw err;
2624
}
2725
}

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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ 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

33+
34+
3435
const { site, customization } = context;
3536
const customIcon = 'icon' in customization.favicon ? customization.favicon.icon : null;
3637

38+
console.log('icon: serveIcon', req.url, customIcon ? 'custom' : 'emoji' in customization.favicon ? 'emoji' : 'fallback');
3739
// If the site has a custom icon, redirect to it
3840
if (customIcon) {
3941
const iconUrl = options.theme === 'light' ? customIcon.light : customIcon.dark;
@@ -47,6 +49,11 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {
4749

4850
const contentTitle = site.title;
4951

52+
// Load the font locally to prevent the shared instance used by ImageResponse.
53+
const font = await fetch(new URL('../fonts/Inter/Inter-Regular.ttf', import.meta.url)).then((res) =>
54+
res.arrayBuffer()
55+
);
56+
5057
return new ImageResponse(
5158
<div
5259
tw={tcls(options.theme === 'light' ? 'bg-white' : 'bg-black', size.boxStyle)}
@@ -56,6 +63,7 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {
5663
display: 'flex',
5764
alignItems: 'center',
5865
justifyContent: 'center',
66+
fontFamily: 'Inter',
5967
}}
6068
>
6169
<h2
@@ -74,6 +82,14 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {
7482
{
7583
width: size.width,
7684
height: size.height,
85+
fonts: [
86+
{
87+
data: font,
88+
name: 'Inter',
89+
weight: 400,
90+
style: 'normal',
91+
}
92+
]
7793
}
7894
);
7995
}

0 commit comments

Comments
 (0)