Skip to content

Commit 7bb7671

Browse files
committed
Comment
1 parent 9fc7fad commit 7bb7671

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

packages/gitbook-v2/src/lib/data/memoize.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getCloudflareContext } from './cloudflare';
99
*/
1010
export function memoize<F extends (...args: any[]) => any>(f: F): F {
1111
const getFunctionCache = async () => {
12-
const functionsCache = await getRequestCacheMap();
12+
const functionsCache = await getRequestCacheWeakMap();
1313
const cache = functionsCache.get(f);
1414
if (cache) {
1515
return cache;
@@ -57,9 +57,11 @@ const globalCache = new WeakMap<any, CacheStorage<string, unknown>>();
5757
const perRequestCache = new WeakMap<object, WeakMap<any, CacheStorage<string, unknown>>>();
5858

5959
/**
60-
* Get the cache storage for the current request.
60+
* Get a global weakmap that is scoped to the current request when executed in Cloudflare Workers,
61+
* to avoid "Cannot perform I/O on behalf of a different request" errors.
62+
* And global when executed in Node.js.
6163
*/
62-
async function getRequestCacheMap(): Promise<WeakMap<any, CacheStorage<string, unknown>>> {
64+
async function getRequestCacheWeakMap(): Promise<WeakMap<any, CacheStorage<string, unknown>>> {
6365
const cloudflareContext = await getCloudflareContext();
6466
if (cloudflareContext) {
6567
// `cf` changes for each request, so we use a per-request cache

0 commit comments

Comments
 (0)