Skip to content

Commit c765463

Browse files
authored
Fix ogimage generation crashing when site is using a custom WOFF2 font (#3119)
1 parent e8ca25b commit c765463

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed

Diff for: .changeset/quick-countries-allow.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gitbook": patch
3+
---
4+
5+
Fix ogimage generation crashing when site is using a custom WOFF2 font

Diff for: packages/gitbook/e2e/customers.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,15 @@ const testCases: TestsCase[] = [
239239
contentBaseURL: 'https://docs.fluentbit.io',
240240
tests: [{ name: 'Home', url: '/', run: waitForCookiesDialog }],
241241
},
242+
{
243+
name: 'run-ai-docs.nvidia.com',
244+
contentBaseURL: 'https://run-ai-docs.nvidia.com',
245+
skip: process.env.ARGOS_BUILD_NAME !== 'customers-v2',
246+
tests: [
247+
{ name: 'Home', url: '/' },
248+
{ name: 'OG Image', url: '/~gitbook/ogimage/h17zQIFwy3MaafVNmItO', mode: 'image' },
249+
],
250+
},
242251
];
243252

244253
runTestCases(testCases);

Diff for: packages/gitbook/e2e/util.ts

+27-13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ export interface Test {
3434
* Test to run
3535
*/
3636
run?: (page: Page, response: Response | null) => Promise<unknown>;
37+
/**
38+
* Mode for the test.
39+
*/
40+
mode?: 'page' | 'image';
3741
/**
3842
* Whether the test should be fullscreened during testing.
3943
*/
@@ -154,6 +158,7 @@ export function runTestCases(testCases: TestsCase[]) {
154158

155159
test.describe(testCase.name, () => {
156160
for (const testEntry of testCase.tests) {
161+
const { mode = 'page' } = testEntry;
157162
const testFn = testEntry.only ? test.only : test;
158163
testFn(testEntry.name, async ({ page, context }) => {
159164
const testEntryPathname =
@@ -194,24 +199,33 @@ export function runTestCases(testCases: TestsCase[]) {
194199
}
195200
const screenshotOptions = testEntry.screenshot;
196201
if (screenshotOptions !== false) {
197-
await argosScreenshot(page, `${testCase.name} - ${testEntry.name}`, {
198-
viewports: ['macbook-16', 'macbook-13', 'ipad-2', 'iphone-x'],
199-
argosCSS: `
202+
const screenshotName = `${testCase.name} - ${testEntry.name}`;
203+
if (mode === 'image') {
204+
await argosScreenshot(page, screenshotName, {
205+
viewports: ['macbook-13'],
206+
threshold: screenshotOptions?.threshold ?? undefined,
207+
fullPage: true,
208+
});
209+
} else {
210+
await argosScreenshot(page, screenshotName, {
211+
viewports: ['macbook-16', 'macbook-13', 'ipad-2', 'iphone-x'],
212+
argosCSS: `
200213
/* Hide Intercom */
201214
.intercom-lightweight-app {
202215
display: none !important;
203216
}
204217
`,
205-
threshold: screenshotOptions?.threshold ?? undefined,
206-
fullPage: testEntry.fullPage ?? false,
207-
beforeScreenshot: async ({ runStabilization }) => {
208-
await runStabilization();
209-
await waitForIcons(page);
210-
if (screenshotOptions?.waitForTOCScrolling !== false) {
211-
await waitForTOCScrolling(page);
212-
}
213-
},
214-
});
218+
threshold: screenshotOptions?.threshold ?? undefined,
219+
fullPage: testEntry.fullPage ?? false,
220+
beforeScreenshot: async ({ runStabilization }) => {
221+
await runStabilization();
222+
await waitForIcons(page);
223+
if (screenshotOptions?.waitForTOCScrolling !== false) {
224+
await waitForTOCScrolling(page);
225+
}
226+
},
227+
});
228+
}
215229
}
216230
});
217231
}

Diff for: packages/gitbook/src/lib/assets.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { GITBOOK_ASSETS_URL } from '@v2/lib/env';
1+
import { GITBOOK_ASSETS_URL, GITBOOK_URL } from '@v2/lib/env';
22

33
/**
44
* Create a public URL for an asset.
55
*/
66
export function getAssetURL(path: string): string {
7-
return `${GITBOOK_ASSETS_URL || ''}/~gitbook/static/${path}`;
7+
return `${GITBOOK_ASSETS_URL || GITBOOK_URL}/~gitbook/static/${path}`;
88
}

Diff for: packages/gitbook/src/routes/ogimage.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,15 @@ export async function serveOGImage(baseContext: GitBookSiteContext, params: Page
8989
await Promise.all(
9090
primaryFontWeights.map((face) => {
9191
const { weight, sources } = face;
92-
if (sources.length === 0) {
92+
const source = sources[0];
93+
94+
// Satori doesn't support WOFF2, so we skip it
95+
// https://github.com/vercel/satori?tab=readme-ov-file#fonts
96+
if (!source || source.format === 'woff2' || source.url.endsWith('.woff2')) {
9397
return null;
9498
}
95-
const url = sources[0].url;
9699

97-
return loadCustomFont({ url, weight });
100+
return loadCustomFont({ url: source.url, weight });
98101
})
99102
)
100103
).filter(filterOutNullable);

0 commit comments

Comments
 (0)