Skip to content

Commit 5bbd254

Browse files
conico974Nicolas Dorseuil
andauthored
Support webp and avif formats for og image (#3478)
Co-authored-by: Nicolas Dorseuil <nicolas@gitbook.io>
1 parent 611e286 commit 5bbd254

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

packages/gitbook/src/lib/images/resizer/resizeImage.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ export async function resizeImage(
4848
input: string,
4949
options: CloudflareImageOptions & {
5050
signal?: AbortSignal;
51+
/**
52+
* Bypass the check to see if the image can be resized.
53+
* This is useful for some format that are not supported by @next/og and need to be transformed
54+
*/
55+
bypassSkipCheck?: boolean;
5156
}
5257
): Promise<Response> {
5358
const action = checkIsSizableImageURL(input);
54-
if (action === SizableImageAction.Skip) {
59+
if (action === SizableImageAction.Skip && !options.bypassSkipCheck) {
5560
throw new Error(
5661
'Cannot resize this image, this function should have never been called on this url'
5762
);

packages/gitbook/src/routes/ogimage.tsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ export async function serveOGImage(baseContext: GitBookSiteContext, params: Page
252252
height: 630,
253253
fonts: fonts.length ? fonts : undefined,
254254
headers: {
255+
// We don't want to cache the image for too long in the browser
256+
'cache-control': 'public, max-age=300, s-maxage=31536000',
255257
'cache-tag': [
256258
getCacheTag({
257259
tag: 'site',
@@ -366,19 +368,22 @@ const SUPPORTED_IMAGE_TYPES = [
366368
async function fetchImage(url: string, options?: ResizeImageOptions) {
367369
// Skip early some images to avoid fetching them
368370
const parsedURL = new URL(url);
369-
if (UNSUPPORTED_IMAGE_EXTENSIONS.includes(getExtension(parsedURL.pathname).toLowerCase())) {
370-
return null;
371-
}
372371

373-
// We use the image resizer to normalize the image format to PNG.
374-
// as @vercel/og can sometimes fail on some JPEG images.
375-
const response =
376-
checkIsSizableImageURL(url) !== SizableImageAction.Resize
377-
? await fetch(url)
378-
: await resizeImage(url, {
379-
...options,
380-
format: 'png',
381-
});
372+
let response: Response;
373+
if (
374+
UNSUPPORTED_IMAGE_EXTENSIONS.includes(getExtension(parsedURL.pathname).toLowerCase()) ||
375+
checkIsSizableImageURL(url) === SizableImageAction.Resize
376+
) {
377+
// We use the image resizer to normalize the image format to PNG.
378+
// as @vercel/og can sometimes fail on some JPEG images, and will fail on avif and webp images.
379+
response = await resizeImage(url, {
380+
...options,
381+
format: 'png',
382+
bypassSkipCheck: true, // Bypass the check to see if the image can be resized
383+
});
384+
} else {
385+
response = await fetch(url);
386+
}
382387

383388
// Filter out unsupported image types
384389
const contentType = response.headers.get('content-type');

0 commit comments

Comments
 (0)