Skip to content

Fix ogimage generation crashing when site is using a custom WOFF2 font #3119

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 5 commits into from
Apr 8, 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
5 changes: 5 additions & 0 deletions .changeset/quick-countries-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gitbook": patch
---

Fix ogimage generation crashing when site is using a custom WOFF2 font
9 changes: 9 additions & 0 deletions packages/gitbook/e2e/customers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ const testCases: TestsCase[] = [
contentBaseURL: 'https://docs.fluentbit.io',
tests: [{ name: 'Home', url: '/', run: waitForCookiesDialog }],
},
{
name: 'run-ai-docs.nvidia.com',
contentBaseURL: 'https://run-ai-docs.nvidia.com',
skip: process.env.ARGOS_BUILD_NAME !== 'customers-v2',
tests: [
{ name: 'Home', url: '/' },
{ name: 'OG Image', url: '/~gitbook/ogimage/h17zQIFwy3MaafVNmItO', mode: 'image' },
],
},
];

runTestCases(testCases);
40 changes: 27 additions & 13 deletions packages/gitbook/e2e/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export interface Test {
* Test to run
*/
run?: (page: Page, response: Response | null) => Promise<unknown>;
/**
* Mode for the test.
*/
mode?: 'page' | 'image';
/**
* Whether the test should be fullscreened during testing.
*/
Expand Down Expand Up @@ -154,6 +158,7 @@ export function runTestCases(testCases: TestsCase[]) {

test.describe(testCase.name, () => {
for (const testEntry of testCase.tests) {
const { mode = 'page' } = testEntry;
const testFn = testEntry.only ? test.only : test;
testFn(testEntry.name, async ({ page, context }) => {
const testEntryPathname =
Expand Down Expand Up @@ -194,24 +199,33 @@ export function runTestCases(testCases: TestsCase[]) {
}
const screenshotOptions = testEntry.screenshot;
if (screenshotOptions !== false) {
await argosScreenshot(page, `${testCase.name} - ${testEntry.name}`, {
viewports: ['macbook-16', 'macbook-13', 'ipad-2', 'iphone-x'],
argosCSS: `
const screenshotName = `${testCase.name} - ${testEntry.name}`;
if (mode === 'image') {
await argosScreenshot(page, screenshotName, {
viewports: ['macbook-13'],
threshold: screenshotOptions?.threshold ?? undefined,
fullPage: true,
});
} else {
await argosScreenshot(page, screenshotName, {
viewports: ['macbook-16', 'macbook-13', 'ipad-2', 'iphone-x'],
argosCSS: `
/* Hide Intercom */
.intercom-lightweight-app {
display: none !important;
}
`,
threshold: screenshotOptions?.threshold ?? undefined,
fullPage: testEntry.fullPage ?? false,
beforeScreenshot: async ({ runStabilization }) => {
await runStabilization();
await waitForIcons(page);
if (screenshotOptions?.waitForTOCScrolling !== false) {
await waitForTOCScrolling(page);
}
},
});
threshold: screenshotOptions?.threshold ?? undefined,
fullPage: testEntry.fullPage ?? false,
beforeScreenshot: async ({ runStabilization }) => {
await runStabilization();
await waitForIcons(page);
if (screenshotOptions?.waitForTOCScrolling !== false) {
await waitForTOCScrolling(page);
}
},
});
}
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/gitbook/src/lib/assets.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { GITBOOK_ASSETS_URL } from '@v2/lib/env';
import { GITBOOK_ASSETS_URL, GITBOOK_URL } from '@v2/lib/env';

/**
* Create a public URL for an asset.
*/
export function getAssetURL(path: string): string {
return `${GITBOOK_ASSETS_URL || ''}/~gitbook/static/${path}`;
return `${GITBOOK_ASSETS_URL || GITBOOK_URL}/~gitbook/static/${path}`;
}
9 changes: 6 additions & 3 deletions packages/gitbook/src/routes/ogimage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,15 @@ export async function serveOGImage(baseContext: GitBookSiteContext, params: Page
await Promise.all(
primaryFontWeights.map((face) => {
const { weight, sources } = face;
if (sources.length === 0) {
const source = sources[0];

// Satori doesn't support WOFF2, so we skip it
// https://github.com/vercel/satori?tab=readme-ov-file#fonts
if (!source || source.format === 'woff2' || source.url.endsWith('.woff2')) {
return null;
}
const url = sources[0].url;

return loadCustomFont({ url, weight });
return loadCustomFont({ url: source.url, weight });
})
)
).filter(filterOutNullable);
Expand Down
Loading