| import type { WorkStore } from '../app-render/work-async-storage.external' |
| import type { IncrementalCache } from '../lib/incremental-cache' |
| import type { RenderOpts } from '../app-render/types' |
| import type { FetchMetric } from '../base-http' |
| import type { RequestLifecycleOpts } from '../base-server' |
| import type { AppSegmentConfig } from '../../build/segment-config/app/app-segment-config' |
| import type { CacheLife } from '../use-cache/cache-life' |
|
|
| import { AfterContext } from '../after/after-context' |
|
|
| import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths' |
| import { createLazyResult, type LazyResult } from '../lib/lazy-result' |
| import { getCacheHandlerEntries } from '../use-cache/handlers' |
| import { createSnapshot } from '../app-render/async-local-storage' |
|
|
| export type WorkStoreContext = { |
| |
| |
| |
| page: string |
|
|
| isPrefetchRequest?: boolean |
| nonce?: string |
| renderOpts: { |
| cacheLifeProfiles?: { [profile: string]: CacheLife } |
| incrementalCache?: IncrementalCache |
| isOnDemandRevalidate?: boolean |
| cacheComponents: boolean |
| fetchCache?: AppSegmentConfig['fetchCache'] |
| isPossibleServerAction?: boolean |
| pendingWaitUntil?: Promise<any> |
| experimental: Pick< |
| RenderOpts['experimental'], |
| 'isRoutePPREnabled' | 'authInterrupts' |
| > |
|
|
| |
| |
| |
| fetchMetrics?: FetchMetric[] |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| store?: WorkStore |
| } & Pick< |
| |
| |
| RenderOpts, |
| | 'assetPrefix' |
| | 'supportsDynamicResponse' |
| | 'shouldWaitOnAllReady' |
| | 'nextExport' |
| | 'isDraftMode' |
| | 'isDebugDynamicAccesses' |
| | 'dev' |
| | 'hasReadableErrorStacks' |
| > & |
| RequestLifecycleOpts & |
| Partial<Pick<RenderOpts, 'reactLoadableManifest'>> |
|
|
| |
| |
| |
| buildId: string |
|
|
| |
| |
| previouslyRevalidatedTags: string[] |
| } |
|
|
| export function createWorkStore({ |
| page, |
| renderOpts, |
| isPrefetchRequest, |
| buildId, |
| previouslyRevalidatedTags, |
| nonce, |
| }: WorkStoreContext): WorkStore { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| const isStaticGeneration = |
| !renderOpts.shouldWaitOnAllReady && |
| !renderOpts.supportsDynamicResponse && |
| !renderOpts.isDraftMode && |
| !renderOpts.isPossibleServerAction |
|
|
| const isDevelopment = renderOpts.dev ?? false |
|
|
| const shouldTrackFetchMetrics = |
| isDevelopment || |
| |
| |
| |
| (isStaticGeneration && |
| (!!process.env.NEXT_DEBUG_BUILD || |
| process.env.NEXT_SSG_FETCH_METRICS === '1')) |
|
|
| const store: WorkStore = { |
| isStaticGeneration, |
| page, |
| route: normalizeAppPath(page), |
| incrementalCache: |
| |
| |
| renderOpts.incrementalCache || (globalThis as any).__incrementalCache, |
| cacheLifeProfiles: renderOpts.cacheLifeProfiles, |
| isBuildTimePrerendering: renderOpts.nextExport, |
| hasReadableErrorStacks: renderOpts.hasReadableErrorStacks, |
| fetchCache: renderOpts.fetchCache, |
| isOnDemandRevalidate: renderOpts.isOnDemandRevalidate, |
|
|
| isDraftMode: renderOpts.isDraftMode, |
|
|
| isPrefetchRequest, |
| buildId, |
| reactLoadableManifest: renderOpts?.reactLoadableManifest || {}, |
| assetPrefix: renderOpts?.assetPrefix || '', |
| nonce, |
|
|
| afterContext: createAfterContext(renderOpts), |
| cacheComponentsEnabled: renderOpts.cacheComponents, |
| dev: isDevelopment, |
| previouslyRevalidatedTags, |
| refreshTagsByCacheKind: createRefreshTagsByCacheKind(), |
| runInCleanSnapshot: createSnapshot(), |
| shouldTrackFetchMetrics, |
| reactServerErrorsByDigest: new Map(), |
| } |
|
|
| |
| renderOpts.store = store |
|
|
| return store |
| } |
|
|
| function createAfterContext(renderOpts: RequestLifecycleOpts): AfterContext { |
| const { waitUntil, onClose, onAfterTaskError } = renderOpts |
| return new AfterContext({ |
| waitUntil, |
| onClose, |
| onTaskError: onAfterTaskError, |
| }) |
| } |
|
|
| |
| |
| |
| |
| function createRefreshTagsByCacheKind(): Map<string, LazyResult<void>> { |
| const refreshTagsByCacheKind = new Map<string, LazyResult<void>>() |
| const cacheHandlers = getCacheHandlerEntries() |
|
|
| if (cacheHandlers) { |
| for (const [kind, cacheHandler] of cacheHandlers) { |
| if ('refreshTags' in cacheHandler) { |
| refreshTagsByCacheKind.set( |
| kind, |
| createLazyResult(async () => cacheHandler.refreshTags()) |
| ) |
| } |
| } |
| } |
|
|
| return refreshTagsByCacheKind |
| } |
|
|