Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/run/handlers/cache.cts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
options: CacheHandlerContext
revalidatedTags: string[]
cacheStore: MemoizedKeyValueStoreBackedByRegionalBlobStore
prerenderManifest?: Promise<PrerenderManifest>
tracer = getTracer()

constructor(options: CacheHandlerContext) {
Expand Down Expand Up @@ -165,6 +166,30 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
}
}

private async getPrerenderManifest(serverDistDir: string): Promise<PrerenderManifest> {
if (this.prerenderManifest) {
return this.prerenderManifest
}

const prerenderManifestPath = join(serverDistDir, '..', 'prerender-manifest.json')

try {
// @ts-expect-error Starting in 15.4.0-canary.10 loadManifest was relocated (https://github.com/vercel/next.js/pull/78358)
// eslint-disable-next-line import/no-unresolved, n/no-missing-import
const { loadManifest } = await import('next/dist/server/load-manifest.external.js')
this.prerenderManifest = Promise.resolve(
loadManifest(prerenderManifestPath) as PrerenderManifest,
)
} catch {
const { loadManifest } = await import('next/dist/server/load-manifest.js')
this.prerenderManifest = Promise.resolve(
loadManifest(prerenderManifestPath) as PrerenderManifest,
)
}

return this.prerenderManifest
}

private async injectEntryToPrerenderManifest(
key: string,
{ revalidate, cacheControl }: Pick<NetlifyCachedPageValue, 'revalidate' | 'cacheControl'>,
Expand All @@ -176,11 +201,7 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
typeof cacheControl !== 'undefined')
) {
try {
const { loadManifest } = await import('next/dist/server/load-manifest.js')
const prerenderManifest = loadManifest(
join(this.options.serverDistDir, '..', 'prerender-manifest.json'),
) as PrerenderManifest

const prerenderManifest = await this.getPrerenderManifest(this.options.serverDistDir)
if (typeof cacheControl !== 'undefined') {
// instead of `revalidate` property, we might get `cacheControls` ( https://github.com/vercel/next.js/pull/76207 )
// then we need to keep track of revalidate values via SharedCacheControls
Expand Down
Loading