Skip to content

Use Inter font when generating icons to prevent ArrayBuffer re-use. #3100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ export const runtime = 'edge';

export async function GET(req: NextRequest) {
try {
console.log('icon: getSiteContentPointer');
const pointer = await getSiteContentPointer();
console.log('icon: fetchV1context');
const context = await fetchV1ContextForSitePointer(pointer);

// biome-ignore lint/suspicious/noConsole: we want to log here
console.log(`serving icon for ${context.site.id}`);

return await serveIcon(context, req);
const response = await serveIcon(context, req);
// biome-ignore lint/suspicious/noConsole: we want to log here
console.log(`served icon for ${context.site.id}`);
return response;
} catch (err) {
if (err instanceof Error) {
console.error(`icon: ${err.toString()}`, err.stack);
}

// biome-ignore lint/suspicious/noConsole: we want to log here
console.log(`serveIcon error: ${err}`, (err as Error).stack);
throw err;
}
}
3 changes: 0 additions & 3 deletions packages/gitbook/src/lib/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ async function getDataFetcherV1(): Promise<GitBookDataFetcher> {
export async function fetchV1ContextForSitePointer(pointer: SiteContentPointer) {
const baseContext = await getV1BaseContext();

console.log('icon: baseContext success');
const context = await fetchSiteContextByIds(baseContext, {
organization: pointer.organizationId,
site: pointer.siteId,
Expand All @@ -296,10 +295,8 @@ export async function fetchV1ContextForSitePointer(pointer: SiteContentPointer)
changeRequest: pointer.changeRequestId,
revision: pointer.revisionId,
});
console.log('icon: context inner success');

context.customization = await getDynamicCustomizationSettings(context.customization);
console.log('icon: customization success');

return context;
}
Expand Down
20 changes: 19 additions & 1 deletion packages/gitbook/src/routes/icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ const SIZES = {
* Generate an icon for a site content.
*/
export async function serveIcon(context: GitBookSiteContext, req: Request) {
console.log('icon: serveIcon', req.url);
const options = getOptions(req.url);
const size = SIZES[options.size];

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

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

const contentTitle = site.title;

// Load the font locally to prevent the shared instance used by ImageResponse.
const font = await fetch(new URL('../fonts/Inter/Inter-Regular.ttf', import.meta.url)).then(
(res) => res.arrayBuffer()
);

return new ImageResponse(
<div
tw={tcls(options.theme === 'light' ? 'bg-white' : 'bg-black', size.boxStyle)}
Expand All @@ -56,6 +65,7 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
fontFamily: 'Inter',
}}
>
<h2
Expand All @@ -74,6 +84,14 @@ export async function serveIcon(context: GitBookSiteContext, req: Request) {
{
width: size.width,
height: size.height,
fonts: [
{
data: font,
name: 'Inter',
weight: 400,
style: 'normal',
},
],
}
);
}
Expand Down