| import type { |
| ResponseCacheEntry, |
| ResponseGenerator, |
| ResponseCacheBase, |
| IncrementalResponseCacheEntry, |
| IncrementalResponseCache, |
| } from './types' |
|
|
| import { Batcher } from '../../lib/batcher' |
| import { scheduleOnNextTick } from '../../lib/scheduler' |
| import { |
| fromResponseCacheEntry, |
| routeKindToIncrementalCacheKind, |
| toResponseCacheEntry, |
| } from './utils' |
| import type { RouteKind } from '../route-kind' |
|
|
| export * from './types' |
|
|
| export default class ResponseCache implements ResponseCacheBase { |
| private readonly batcher = Batcher.create< |
| { key: string; isOnDemandRevalidate: boolean }, |
| IncrementalResponseCacheEntry | null, |
| string |
| >({ |
| |
| |
| cacheKeyFn: ({ key, isOnDemandRevalidate }) => |
| `${key}-${isOnDemandRevalidate ? '1' : '0'}`, |
| |
| |
| |
| schedulerFn: scheduleOnNextTick, |
| }) |
|
|
| private previousCacheItem?: { |
| key: string |
| entry: IncrementalResponseCacheEntry | null |
| expiresAt: number |
| } |
|
|
| |
| |
| |
| private minimal_mode?: boolean |
|
|
| constructor(minimal_mode: boolean) { |
| this.minimal_mode = minimal_mode |
| } |
|
|
| public async get( |
| key: string | null, |
| responseGenerator: ResponseGenerator, |
| context: { |
| routeKind: RouteKind |
| isOnDemandRevalidate?: boolean |
| isPrefetch?: boolean |
| incrementalCache: IncrementalResponseCache |
| isRoutePPREnabled?: boolean |
| isFallback?: boolean |
| waitUntil?: (prom: Promise<any>) => void |
| } |
| ): Promise<ResponseCacheEntry | null> { |
| |
| |
| if (!key) { |
| return responseGenerator({ hasResolved: false, previousCacheEntry: null }) |
| } |
|
|
| const { |
| incrementalCache, |
| isOnDemandRevalidate = false, |
| isFallback = false, |
| isRoutePPREnabled = false, |
| waitUntil, |
| } = context |
|
|
| const response = await this.batcher.batch( |
| { key, isOnDemandRevalidate }, |
| (cacheKey, resolve) => { |
| const prom = (async () => { |
| |
| |
| if ( |
| this.minimal_mode && |
| this.previousCacheItem?.key === cacheKey && |
| this.previousCacheItem.expiresAt > Date.now() |
| ) { |
| return this.previousCacheItem.entry |
| } |
|
|
| |
| const kind = routeKindToIncrementalCacheKind(context.routeKind) |
|
|
| let resolved = false |
| let cachedResponse: IncrementalResponseCacheEntry | null = null |
| try { |
| cachedResponse = !this.minimal_mode |
| ? await incrementalCache.get(key, { |
| kind, |
| isRoutePPREnabled: context.isRoutePPREnabled, |
| isFallback, |
| }) |
| : null |
|
|
| if (cachedResponse && !isOnDemandRevalidate) { |
| resolve(cachedResponse) |
| resolved = true |
|
|
| if (!cachedResponse.isStale || context.isPrefetch) { |
| |
| |
| return null |
| } |
| } |
|
|
| const cacheEntry = await responseGenerator({ |
| hasResolved: resolved, |
| previousCacheEntry: cachedResponse, |
| isRevalidating: true, |
| }) |
|
|
| |
| |
| if (!cacheEntry) { |
| |
| if (this.minimal_mode) this.previousCacheItem = undefined |
| return null |
| } |
|
|
| const resolveValue = await fromResponseCacheEntry({ |
| ...cacheEntry, |
| isMiss: !cachedResponse, |
| }) |
| if (!resolveValue) { |
| |
| if (this.minimal_mode) this.previousCacheItem = undefined |
| return null |
| } |
|
|
| |
| |
| if (!isOnDemandRevalidate && !resolved) { |
| resolve(resolveValue) |
| resolved = true |
| } |
|
|
| |
| |
| if (resolveValue.cacheControl) { |
| if (this.minimal_mode) { |
| this.previousCacheItem = { |
| key: cacheKey, |
| entry: resolveValue, |
| expiresAt: Date.now() + 1000, |
| } |
| } else { |
| await incrementalCache.set(key, resolveValue.value, { |
| cacheControl: resolveValue.cacheControl, |
| isRoutePPREnabled, |
| isFallback, |
| }) |
| } |
| } |
|
|
| return resolveValue |
| } catch (err) { |
| |
| |
| if (cachedResponse?.cacheControl) { |
| const newRevalidate = Math.min( |
| Math.max(cachedResponse.cacheControl.revalidate || 3, 3), |
| 30 |
| ) |
|
|
| const newExpire = |
| cachedResponse.cacheControl.expire === undefined |
| ? undefined |
| : Math.max( |
| newRevalidate + 3, |
| cachedResponse.cacheControl.expire |
| ) |
|
|
| await incrementalCache.set(key, cachedResponse.value, { |
| cacheControl: { revalidate: newRevalidate, expire: newExpire }, |
| isRoutePPREnabled, |
| isFallback, |
| }) |
| } |
|
|
| |
| |
| if (resolved) { |
| console.error(err) |
| return null |
| } |
|
|
| |
| throw err |
| } |
| })() |
|
|
| |
| |
| if (waitUntil) { |
| waitUntil(prom) |
| } |
| return prom |
| } |
| ) |
|
|
| return toResponseCacheEntry(response) |
| } |
| } |
|
|