Skip to content

Commit

Permalink
Update global cache handler ref (vercel#71263)
Browse files Browse the repository at this point in the history
This moves this out of the request context as it can be it's own global
symbol instead.
  • Loading branch information
ijjk authored Oct 14, 2024
1 parent 982392e commit 61c9988
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions packages/next/src/server/lib/incremental-cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
} from '../../../lib/constants'
import { toRoute } from '../to-route'
import { SharedRevalidateTimings } from './shared-revalidate-timings'
import { getBuiltinRequestContext } from '../../after/builtin-request-context'

export interface CacheHandlerContext {
fs?: CacheFs
Expand All @@ -44,6 +43,8 @@ export interface CacheHandlerValue {
value: IncrementalCacheValue | null
}

export const cacheHandlersSymbol = Symbol('@next/cache-handlers')

export class CacheHandler {
// eslint-disable-next-line
constructor(_ctx: CacheHandlerContext) {}
Expand Down Expand Up @@ -122,28 +123,35 @@ export class IncrementalCache implements IncrementalCacheType {
const debug = !!process.env.NEXT_PRIVATE_DEBUG_CACHE
this.hasCustomCacheHandler = Boolean(CurCacheHandler)

const globalCacheHandler = getBuiltinRequestContext()?.NextCacheHandler

if (globalCacheHandler) {
CurCacheHandler = globalCacheHandler
}
const _globalThis: typeof globalThis & {
[cacheHandlersSymbol]?: {
FetchCache?: typeof CacheHandler
}
} = globalThis

if (!CurCacheHandler) {
if (fs && serverDistDir) {
if (debug) {
console.log('using filesystem cache handler')
// if we have a global cache handler available leverage it
const globalCacheHandler = _globalThis[cacheHandlersSymbol]

if (globalCacheHandler?.FetchCache) {
CurCacheHandler = globalCacheHandler.FetchCache
} else {
if (fs && serverDistDir) {
if (debug) {
console.log('using filesystem cache handler')
}
CurCacheHandler = FileSystemCache
}
CurCacheHandler = FileSystemCache
}
if (
FetchCache.isAvailable({ _requestHeaders: requestHeaders }) &&
minimalMode &&
fetchCache
) {
if (debug) {
console.log('using fetch cache handler')
if (
FetchCache.isAvailable({ _requestHeaders: requestHeaders }) &&
minimalMode &&
fetchCache
) {
if (debug) {
console.log('using fetch cache handler')
}
CurCacheHandler = FetchCache
}
CurCacheHandler = FetchCache
}
} else if (debug) {
console.log('using custom cache handler', CurCacheHandler.name)
Expand Down

0 comments on commit 61c9988

Please sign in to comment.