Skip to content

Commit 61c9988

Browse files
authored
Update global cache handler ref (#71263)
This moves this out of the request context as it can be it's own global symbol instead.
1 parent 982392e commit 61c9988

File tree

1 file changed

+27
-19
lines changed
  • packages/next/src/server/lib/incremental-cache

1 file changed

+27
-19
lines changed

packages/next/src/server/lib/incremental-cache/index.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
} from '../../../lib/constants'
2424
import { toRoute } from '../to-route'
2525
import { SharedRevalidateTimings } from './shared-revalidate-timings'
26-
import { getBuiltinRequestContext } from '../../after/builtin-request-context'
2726

2827
export interface CacheHandlerContext {
2928
fs?: CacheFs
@@ -44,6 +43,8 @@ export interface CacheHandlerValue {
4443
value: IncrementalCacheValue | null
4544
}
4645

46+
export const cacheHandlersSymbol = Symbol('@next/cache-handlers')
47+
4748
export class CacheHandler {
4849
// eslint-disable-next-line
4950
constructor(_ctx: CacheHandlerContext) {}
@@ -122,28 +123,35 @@ export class IncrementalCache implements IncrementalCacheType {
122123
const debug = !!process.env.NEXT_PRIVATE_DEBUG_CACHE
123124
this.hasCustomCacheHandler = Boolean(CurCacheHandler)
124125

125-
const globalCacheHandler = getBuiltinRequestContext()?.NextCacheHandler
126-
127-
if (globalCacheHandler) {
128-
CurCacheHandler = globalCacheHandler
129-
}
126+
const _globalThis: typeof globalThis & {
127+
[cacheHandlersSymbol]?: {
128+
FetchCache?: typeof CacheHandler
129+
}
130+
} = globalThis
130131

131132
if (!CurCacheHandler) {
132-
if (fs && serverDistDir) {
133-
if (debug) {
134-
console.log('using filesystem cache handler')
133+
// if we have a global cache handler available leverage it
134+
const globalCacheHandler = _globalThis[cacheHandlersSymbol]
135+
136+
if (globalCacheHandler?.FetchCache) {
137+
CurCacheHandler = globalCacheHandler.FetchCache
138+
} else {
139+
if (fs && serverDistDir) {
140+
if (debug) {
141+
console.log('using filesystem cache handler')
142+
}
143+
CurCacheHandler = FileSystemCache
135144
}
136-
CurCacheHandler = FileSystemCache
137-
}
138-
if (
139-
FetchCache.isAvailable({ _requestHeaders: requestHeaders }) &&
140-
minimalMode &&
141-
fetchCache
142-
) {
143-
if (debug) {
144-
console.log('using fetch cache handler')
145+
if (
146+
FetchCache.isAvailable({ _requestHeaders: requestHeaders }) &&
147+
minimalMode &&
148+
fetchCache
149+
) {
150+
if (debug) {
151+
console.log('using fetch cache handler')
152+
}
153+
CurCacheHandler = FetchCache
145154
}
146-
CurCacheHandler = FetchCache
147155
}
148156
} else if (debug) {
149157
console.log('using custom cache handler', CurCacheHandler.name)

0 commit comments

Comments
 (0)