Skip to content

Commit a80d61e

Browse files
lubieowoceunstubbable
authored andcommitted
fix: make sure the ALS for createCacheScope is a singleton
1 parent 3cfce00 commit a80d61e

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createAsyncLocalStorage } from './async-local-storage'
2+
import type { CacheScopeStorageAsyncStorage } from './cache-scope-storage.external'
3+
4+
export const cacheScopeAsyncStorage: CacheScopeStorageAsyncStorage =
5+
createAsyncLocalStorage()
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { AsyncLocalStorage } from 'async_hooks'
2+
3+
// Share the instance module in the next-shared layer
4+
import { cacheScopeAsyncStorage } from './cache-scope-storage-instance' with { 'turbopack-transition': 'next-shared' }
5+
6+
type CacheMap = Map<Function, unknown>
7+
8+
export type CacheScopeStorageAsyncStorage = AsyncLocalStorage<CacheMap>
9+
10+
export { cacheScopeAsyncStorage }

packages/next/src/server/after/react-cache-scope.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { AsyncLocalStorage } from 'async_hooks'
21
import { InvariantError } from '../../shared/lib/invariant-error'
2+
import { cacheScopeAsyncStorage } from '../../client/components/cache-scope-storage.external'
33

44
export function createCacheScope() {
55
const storage = createCacheMap()
66
return {
77
run: <T>(callback: () => T): T => {
8-
return CacheScopeStorage.run(storage, () => callback())
8+
return cacheScopeAsyncStorage.run(storage, () => callback())
99
},
1010
}
1111
}
@@ -33,15 +33,12 @@ function createCacheMap(): CacheMap {
3333
}
3434

3535
function isWithinCacheScope() {
36-
return !!CacheScopeStorage.getStore()
36+
return !!cacheScopeAsyncStorage.getStore()
3737
}
3838

39-
const CacheScopeStorage: AsyncLocalStorage<CacheMap> =
40-
new AsyncLocalStorage<CacheMap>()
41-
4239
/** forked from packages/react-server/src/flight/ReactFlightServerCache.js */
4340
function resolveCache(): CacheMap {
44-
const store = CacheScopeStorage.getStore()
41+
const store = cacheScopeAsyncStorage.getStore()
4542
if (store) {
4643
return store
4744
}

0 commit comments

Comments
 (0)