Fix OGImage icon fetch on cloudflare (#3253)

Co-authored-by: Nicolas Dorseuil <nicolas@gitbook.io>
This commit is contained in:
conico974
2025-05-21 15:39:52 +02:00
committed by GitHub
parent fae34e56ac
commit 19c20c0be3
+2 -20
View File
@@ -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<string, string>();
* 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;
}