Skip to content

Commit

Permalink
Update cache method handling during build (#49633)
Browse files Browse the repository at this point in the history
This ensures we properly handle fetch cache during build.
  • Loading branch information
ijjk committed May 11, 2023
1 parent ce746ef commit f322247
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/next/src/build/utils.ts
Expand Up @@ -63,6 +63,7 @@ import { IncrementalCache } from '../server/lib/incremental-cache'
import { patchFetch } from '../server/lib/patch-fetch'
import { nodeFs } from '../server/lib/node-fs-methods'
import '../server/node-environment'
import * as ciEnvironment from '../telemetry/ci-info'

export type ROUTER_TYPE = 'pages' | 'app'

Expand Down Expand Up @@ -1216,6 +1217,7 @@ export async function buildAppStaticPaths({
}),
CurCacheHandler: CacheHandler,
requestHeaders,
minimalMode: ciEnvironment.hasNextSupport,
})

return StaticGenerationAsyncStorageWrapper.wrap(
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/export/worker.ts
Expand Up @@ -371,6 +371,7 @@ export default async function exportPage({
},
serverDistDir: join(distDir, 'server'),
CurCacheHandler: CacheHandler,
minimalMode: ciEnvironment.hasNextSupport,
})
;(globalThis as any).__incrementalCache = incrementalCache
curRenderOpts.incrementalCache = incrementalCache
Expand Down
19 changes: 15 additions & 4 deletions packages/next/src/server/lib/incremental-cache/fetch-cache.ts
Expand Up @@ -34,10 +34,21 @@ export default class FetchCache implements CacheHandler {
}
delete ctx._requestHeaders[FETCH_CACHE_HEADER]
}
if (ctx._requestHeaders['x-vercel-sc-host']) {
this.cacheEndpoint = `https://${ctx._requestHeaders['x-vercel-sc-host']}${
ctx._requestHeaders['x-vercel-sc-basepath'] || ''
}`
const scHost =
ctx._requestHeaders['x-vercel-sc-host'] || process.env.SUSPENSE_CACHE_URL

const scBasePath =
ctx._requestHeaders['x-vercel-sc-basepath'] ||
process.env.SUSPENSE_CACHE_BASEPATH

if (process.env.SUSPENSE_CACHE_AUTH_TOKEN) {
this.headers[
'Authorization'
] = `Bearer ${process.env.SUSPENSE_CACHE_AUTH_TOKEN}`
}

if (scHost) {
this.cacheEndpoint = `https://${scHost}${scBasePath || ''}`
if (this.debug) {
console.log('using cache endpoint', this.cacheEndpoint)
}
Expand Down

0 comments on commit f322247

Please sign in to comment.