diff --git a/src/lib/cache/memory.ts b/src/lib/cache/memory.ts index a520b785b..15654aadc 100644 --- a/src/lib/cache/memory.ts +++ b/src/lib/cache/memory.ts @@ -1,6 +1,6 @@ -import { CacheBackend, CacheEntry } from './types'; +import { CacheBackend } from './types'; import { NON_IMMUTABLE_LOCAL_CACHE_MAX_AGE_SECONDS, isCacheEntryImmutable } from './utils'; -import { getGlobalContext } from '../waitUntil'; +import { singleton } from '../async'; export const memoryCache: CacheBackend = { name: 'memory', @@ -66,13 +66,7 @@ export const memoryCache: CacheBackend = { /** * With next-on-pages, the code seems to be isolated between the middleware and the handler. * To share the cache between the two, we use a global variable. + * By using a singleton, we ensure that the cache is only created once and stored in the + * current request context. */ -async function getMemoryCache(): Promise> { - let globalThisForMemoryCache: any = await getGlobalContext(); - - if (!globalThisForMemoryCache.gitbookMemoryCache) { - globalThisForMemoryCache.gitbookMemoryCache = new Map(); - } - - return globalThisForMemoryCache.gitbookMemoryCache; -} +const getMemoryCache = singleton(async () => new Map()); diff --git a/src/lib/waitUntil.ts b/src/lib/waitUntil.ts index 730fbc2cc..43ea13aee 100644 --- a/src/lib/waitUntil.ts +++ b/src/lib/waitUntil.ts @@ -12,7 +12,7 @@ export async function getGlobalContext(): Promise { // We lazy-load the next-on-pages package to avoid errors when running tests because of 'server-only'. const { getOptionalRequestContext } = await import('@cloudflare/next-on-pages'); - return getOptionalRequestContext()?.ctx ?? globalThis; + return getOptionalRequestContext()?.cf ?? globalThis; } /**