Skip to content

Commit 19c20c0

Browse files
conico974Nicolas Dorseuil
and
Nicolas Dorseuil
authored
Fix OGImage icon fetch on cloudflare (#3253)
Co-authored-by: Nicolas Dorseuil <[email protected]>
1 parent fae34e5 commit 19c20c0

File tree

1 file changed

+2
-20
lines changed

1 file changed

+2
-20
lines changed

packages/gitbook/src/routes/ogimage.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { getAssetURL } from '@/lib/assets';
99
import { filterOutNullable } from '@/lib/typescript';
1010
import { getCacheTag } from '@gitbook/cache-tags';
1111
import type { GitBookSiteContext } from '@v2/lib/context';
12-
import { getCloudflareContext } from '@v2/lib/data/cloudflare';
1312
import { getResizedImageURL } from '@v2/lib/images';
1413

1514
const googleFontsMap: { [fontName in CustomizationDefaultFont]: string } = {
@@ -322,24 +321,6 @@ function logOnCloudflareOnly(message: string) {
322321
}
323322
}
324323

325-
/**
326-
* Fetch a resource from the function itself.
327-
* To avoid error with worker to worker requests in the same zone, we use the `WORKER_SELF_REFERENCE` binding.
328-
*/
329-
async function fetchSelf(url: string) {
330-
const cloudflare = getCloudflareContext();
331-
if (cloudflare?.env.WORKER_SELF_REFERENCE) {
332-
logOnCloudflareOnly(`Fetching self: ${url}`);
333-
return await cloudflare.env.WORKER_SELF_REFERENCE.fetch(
334-
// `getAssetURL` can return a relative URL, so we need to make it absolute
335-
// the URL doesn't matter, as we're using the worker-self-reference binding
336-
new URL(url, 'https://worker-self-reference/')
337-
);
338-
}
339-
340-
return await fetch(url);
341-
}
342-
343324
/**
344325
* Read an image from a response as a base64 encoded string.
345326
*/
@@ -363,6 +344,7 @@ const staticImagesCache = new Map<string, string>();
363344
* Read a static image and cache it in memory.
364345
*/
365346
async function readStaticImage(url: string) {
347+
logOnCloudflareOnly(`Reading static image: ${url}, cache size: ${staticImagesCache.size}`);
366348
const cached = staticImagesCache.get(url);
367349
if (cached) {
368350
return cached;
@@ -377,7 +359,7 @@ async function readStaticImage(url: string) {
377359
* Read an image from GitBook itself.
378360
*/
379361
async function readSelfImage(url: string) {
380-
const response = await fetchSelf(url);
362+
const response = await fetch(url);
381363
const image = await readImage(response);
382364
return image;
383365
}

0 commit comments

Comments
 (0)