File tree Expand file tree Collapse file tree 2 files changed +6
-12
lines changed Expand file tree Collapse file tree 2 files changed +6
-12
lines changed Original file line number Diff line number Diff line change 1
- import { CacheBackend , CacheEntry } from './types' ;
1
+ import { CacheBackend } from './types' ;
2
2
import { NON_IMMUTABLE_LOCAL_CACHE_MAX_AGE_SECONDS , isCacheEntryImmutable } from './utils' ;
3
- import { getGlobalContext } from '../waitUntil ' ;
3
+ import { singleton } from '../async ' ;
4
4
5
5
export const memoryCache : CacheBackend = {
6
6
name : 'memory' ,
@@ -66,13 +66,7 @@ export const memoryCache: CacheBackend = {
66
66
/**
67
67
* With next-on-pages, the code seems to be isolated between the middleware and the handler.
68
68
* To share the cache between the two, we use a global variable.
69
+ * By using a singleton, we ensure that the cache is only created once and stored in the
70
+ * current request context.
69
71
*/
70
- async function getMemoryCache ( ) : Promise < Map < string , CacheEntry > > {
71
- let globalThisForMemoryCache : any = await getGlobalContext ( ) ;
72
-
73
- if ( ! globalThisForMemoryCache . gitbookMemoryCache ) {
74
- globalThisForMemoryCache . gitbookMemoryCache = new Map ( ) ;
75
- }
76
-
77
- return globalThisForMemoryCache . gitbookMemoryCache ;
78
- }
72
+ const getMemoryCache = singleton ( async ( ) => new Map ( ) ) ;
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ export async function getGlobalContext(): Promise<object> {
12
12
13
13
// We lazy-load the next-on-pages package to avoid errors when running tests because of 'server-only'.
14
14
const { getOptionalRequestContext } = await import ( '@cloudflare/next-on-pages' ) ;
15
- return getOptionalRequestContext ( ) ?. ctx ?? globalThis ;
15
+ return getOptionalRequestContext ( ) ?. cf ?? globalThis ;
16
16
}
17
17
18
18
/**
You can’t perform that action at this time.
0 commit comments