From 19c20c0be35374d7126db17009a40d313795ced8 Mon Sep 17 00:00:00 2001 From: conico974 Date: Wed, 21 May 2025 15:39:52 +0200 Subject: [PATCH] Fix OGImage icon fetch on cloudflare (#3253) Co-authored-by: Nicolas Dorseuil --- packages/gitbook/src/routes/ogimage.tsx | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/packages/gitbook/src/routes/ogimage.tsx b/packages/gitbook/src/routes/ogimage.tsx index c1cfd9772..ee586cb1b 100644 --- a/packages/gitbook/src/routes/ogimage.tsx +++ b/packages/gitbook/src/routes/ogimage.tsx @@ -9,7 +9,6 @@ import { getAssetURL } from '@/lib/assets'; import { filterOutNullable } from '@/lib/typescript'; import { getCacheTag } from '@gitbook/cache-tags'; import type { GitBookSiteContext } from '@v2/lib/context'; -import { getCloudflareContext } from '@v2/lib/data/cloudflare'; import { getResizedImageURL } from '@v2/lib/images'; const googleFontsMap: { [fontName in CustomizationDefaultFont]: string } = { @@ -322,24 +321,6 @@ function logOnCloudflareOnly(message: string) { } } -/** - * Fetch a resource from the function itself. - * To avoid error with worker to worker requests in the same zone, we use the `WORKER_SELF_REFERENCE` binding. - */ -async function fetchSelf(url: string) { - const cloudflare = getCloudflareContext(); - if (cloudflare?.env.WORKER_SELF_REFERENCE) { - logOnCloudflareOnly(`Fetching self: ${url}`); - return await cloudflare.env.WORKER_SELF_REFERENCE.fetch( - // `getAssetURL` can return a relative URL, so we need to make it absolute - // the URL doesn't matter, as we're using the worker-self-reference binding - new URL(url, 'https://worker-self-reference/') - ); - } - - return await fetch(url); -} - /** * Read an image from a response as a base64 encoded string. */ @@ -363,6 +344,7 @@ const staticImagesCache = new Map(); * Read a static image and cache it in memory. */ async function readStaticImage(url: string) { + logOnCloudflareOnly(`Reading static image: ${url}, cache size: ${staticImagesCache.size}`); const cached = staticImagesCache.get(url); if (cached) { return cached; @@ -377,7 +359,7 @@ async function readStaticImage(url: string) { * Read an image from GitBook itself. */ async function readSelfImage(url: string) { - const response = await fetchSelf(url); + const response = await fetch(url); const image = await readImage(response); return image; }