Skip to content

Commit 34bfd22

Browse files
committed
refactor
1 parent d789e0c commit 34bfd22

File tree

6 files changed

+35
-55
lines changed

6 files changed

+35
-55
lines changed

.github/composite/deploy-cloudflare/action.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ runs:
6969
env:
7070
DEPLOYMENT_URL: ${{ steps.deploy.outputs.deployment-url }}
7171
run: |
72-
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"
72+
echo "URL: ${{ steps.deploy.outputs.deployment-url }}"

packages/gitbook/src/components/RootLayout/CustomizationRootLayout.tsx

+10-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,16 @@ export async function CustomizationRootLayout(props: {
5555
// Preconnect and preload custom fonts if needed
5656
if (fontData.type === 'custom') {
5757
ReactDOM.preconnect(GITBOOK_FONTS_URL);
58-
fontData.preloadSources.forEach(({ url }) => {
59-
ReactDOM.preload(url, { as: 'font', crossOrigin: 'anonymous', fetchPriority: 'high' });
60-
});
58+
fontData.preloadSources
59+
.flatMap((face) => face.sources)
60+
.forEach(({ url, format }) => {
61+
ReactDOM.preload(url, {
62+
as: 'font',
63+
crossOrigin: 'anonymous',
64+
fetchPriority: 'high',
65+
type: format ? `font/${format}` : undefined,
66+
});
67+
});
6168
}
6269

6370
return (

packages/gitbook/src/fonts/custom.test.ts

+3-14
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,10 @@ describe('generateFontFacesCSS', () => {
259259
});
260260
});
261261

262-
describe('getCustomFontPreloadLinks', () => {
262+
describe('getFontSourcesToPreload', () => {
263263
const preloadTestCases = [
264264
{ name: 'basic case', font: TEST_FONTS.basic, expectedCount: 2 },
265-
{ name: 'multiple weights', font: TEST_FONTS.multiWeight, expectedCount: 5 },
265+
{ name: 'multiple weights', font: TEST_FONTS.multiWeight, expectedCount: 2 },
266266
{ name: 'multiple sources', font: TEST_FONTS.multiSource, expectedCount: 2 },
267267
{ name: 'missing format', font: TEST_FONTS.missingFormat, expectedCount: 2 },
268268
{ name: 'empty font faces', font: TEST_FONTS.empty, expectedCount: 0 },
@@ -272,20 +272,9 @@ describe('getCustomFontPreloadLinks', () => {
272272

273273
preloadTestCases.forEach(({ name, font, expectedCount }) => {
274274
test(`extracts ${expectedCount} URLs from ${name}`, () => {
275-
const result = getFontSourcesToPreload(font);
275+
const result = getFontSourcesToPreload(font).flatMap((face) => face.sources);
276276
expect(result).toBeArray();
277277
expect(result.length).toBe(expectedCount);
278278
});
279279
});
280-
281-
test('handles different URL types correctly', () => {
282-
const result = getFontSourcesToPreload(TEST_FONTS.variousURLs);
283-
284-
expect(result).toBeArray();
285-
expect(result.length).toBe(2);
286-
expect(result).toMatchObject([
287-
{ url: 'https://example.com/fonts.woff2' },
288-
{ url: 'https://example.com/fonts.woff' },
289-
]);
290-
});
291280
});

packages/gitbook/src/fonts/custom.ts

+8-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { CustomizationFontDefinition, FontSource } from '@gitbook/api';
1+
import type { CustomizationFontDefinition } from '@gitbook/api';
22

33
/**
4-
* Define the custom font faces and set the --font-content to the custom font name
4+
* Define the custom font faces and set the --font-custom to the custom font name
55
*/
66
export function generateFontFacesCSS(customFont: CustomizationFontDefinition): string {
77
const { fontFaces } = customFont;
@@ -21,7 +21,7 @@ export function generateFontFacesCSS(customFont: CustomizationFontDefinition): s
2121
})
2222
.join(', ');
2323

24-
// We could use the font-family name here, but to avoid extra normalization we're using CustomFont
24+
// We could use the font-family name here, but to avoid extra normalization we're using 'CustomFont'
2525
return `
2626
@font-face {
2727
font-family: CustomFont;
@@ -48,18 +48,9 @@ export function generateFontFacesCSS(customFont: CustomizationFontDefinition): s
4848
* Currently we're preloading all sources but we could optimize this in the future by only preloading the important ones
4949
* to avoid blocking the page load.
5050
*/
51-
export function getFontSourcesToPreload(customFont: CustomizationFontDefinition): FontSource[] {
52-
const allSources = customFont.fontFaces.flatMap((face) => face.sources);
53-
54-
const uniqueSources = new Map<string, FontSource>();
55-
56-
// Add each source to the map, using URL as the key
57-
allSources.forEach((source) => {
58-
const url = source.url.toString();
59-
if (!uniqueSources.has(url)) {
60-
uniqueSources.set(url, source);
61-
}
62-
});
63-
64-
return Array.from(uniqueSources.values());
51+
export function getFontSourcesToPreload(customFont: CustomizationFontDefinition) {
52+
return customFont.fontFaces.filter(
53+
(face): face is typeof face & { weight: 400 | 700 } =>
54+
face.weight === 400 || face.weight === 700
55+
);
6556
}

packages/gitbook/src/fonts/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CustomizationFont, FontSource } from '@gitbook/api';
1+
import type { CustomizationFont, CustomizationFontDefinition } from '@gitbook/api';
22
import { generateFontFacesCSS, getFontSourcesToPreload } from './custom';
33
import { fonts } from './default';
44

@@ -21,7 +21,7 @@ interface DefaultFontData {
2121
interface CustomFontData {
2222
type: 'custom';
2323
fontFaceRules: string;
24-
preloadSources: FontSource[];
24+
preloadSources: CustomizationFontDefinition['fontFaces'];
2525
}
2626

2727
/**

packages/gitbook/src/routes/ogimage.tsx

+11-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { redirect } from 'next/navigation';
44
import { ImageResponse } from 'next/og';
55

66
import { type PageParams, fetchPageData } from '@/components/SitePage';
7+
import { getFontSourcesToPreload } from '@/fonts/custom';
78
import { getAssetURL } from '@/lib/assets';
89
import { filterOutNullable } from '@/lib/typescript';
910
import type { GitBookSiteContext } from '@v2/lib/context';
@@ -79,27 +80,19 @@ export async function serveOGImage(baseContext: GitBookSiteContext, params: Page
7980
}
8081

8182
// custom fonts
82-
const primaryFontSources = customization.styling.font.fontFaces
83-
.filter((face): face is typeof face & { weight: 400 | 700 } => {
84-
return face.weight === 400 || face.weight === 700;
85-
})
86-
.map((face) => {
87-
if (face.sources.length === 0) {
88-
return null;
89-
}
90-
91-
return {
92-
weight: face.weight,
93-
// just load the first source, fromat is not that important here
94-
url: face.sources[0].url,
95-
} as const;
96-
})
97-
.filter(filterOutNullable);
83+
// We only load the primary font weights for now
84+
const primaryFontWeights = getFontSourcesToPreload(customization.styling.font);
9885

9986
const fonts = (
10087
await Promise.all(
101-
primaryFontSources.map((source) => {
102-
return loadCustomFont({ url: source.url, weight: source.weight });
88+
primaryFontWeights.map((face) => {
89+
const { weight, sources } = face;
90+
if (sources.length === 0) {
91+
return null;
92+
}
93+
const url = sources[0].url;
94+
95+
return loadCustomFont({ url, weight });
10396
})
10497
)
10598
).filter(filterOutNullable);

0 commit comments

Comments
 (0)