Skip to content

Commit 66bb938

Browse files
authored
Use Inter font when generating icons to prevent ArrayBuffer re-use. (#3101)
1 parent 0e6d928 commit 66bb938

File tree

2 files changed

+11
-8
lines changed
  • packages/gitbook/src

2 files changed

+11
-8
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ export async function GET(req: NextRequest) {
1313

1414
// biome-ignore lint/suspicious/noConsole: we want to log here
1515
console.log(`serving icon for ${context.site.id}`);
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;
16+
return await serveIcon(context, req);
2017
} catch (err) {
2118
// biome-ignore lint/suspicious/noConsole: we want to log here
2219
console.log(`serveIcon error: ${err}`, (err as Error).stack);

packages/gitbook/src/routes/icon.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,15 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {
5252
const contentTitle = site.title;
5353

5454
// Load the font locally to prevent the shared instance used by ImageResponse.
55-
const font = await fetch(new URL('../fonts/Inter/Inter-Regular.ttf', import.meta.url)).then(
56-
(res) => res.arrayBuffer()
57-
);
55+
const fontOrigin = await fetch(
56+
new URL('../fonts/Inter/Inter-Regular.ttf', import.meta.url)
57+
).then((res) => res.arrayBuffer());
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+
}
5864

5965
return new ImageResponse(
6066
<div
@@ -86,7 +92,7 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {
8692
height: size.height,
8793
fonts: [
8894
{
89-
data: font,
95+
data: dst,
9096
name: 'Inter',
9197
weight: 400,
9298
style: 'normal',

0 commit comments

Comments
 (0)