Skip to content

Commit 4f2b2de

Browse files
authored
Only share incremental cache for edge in next start (#79386)
When in minimal mode we should not try leveraging a shared `IncrementalCache` instance across invocations as we pass in request headers which are request specific. The only time we should share this instance is in `next start` mode where the sandbox needs to be able to access the filesystem-cache. x-ref: [slack thread](https://vercel.slack.com/archives/C07CGAA2RS6/p1747687927600699?thread_ts=1747142004.153739&cid=C07CGAA2RS6)
1 parent d16d9f4 commit 4f2b2de

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

packages/next/src/server/base-server.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,8 @@ export default abstract class Server<
14671467

14681468
incrementalCache.resetRequestCache()
14691469
addRequestMeta(req, 'incrementalCache', incrementalCache)
1470+
// This is needed for pages router to leverage unstable_cache
1471+
// TODO: re-work this handling to not use global and use a AsyncStore
14701472
;(globalThis as any).__incrementalCache = incrementalCache
14711473
}
14721474

@@ -2457,13 +2459,15 @@ export default abstract class Server<
24572459

24582460
// use existing incrementalCache instance if available
24592461
const incrementalCache: import('./lib/incremental-cache').IncrementalCache =
2460-
(globalThis as any).__incrementalCache ||
2461-
(await this.getIncrementalCache({
2462-
requestHeaders: Object.assign({}, req.headers),
2463-
requestProtocol: protocol.substring(0, protocol.length - 1) as
2464-
| 'http'
2465-
| 'https',
2466-
}))
2462+
process.env.NEXT_RUNTIME === 'edge' &&
2463+
(globalThis as any).__incrementalCache
2464+
? (globalThis as any).__incrementalCache
2465+
: await this.getIncrementalCache({
2466+
requestHeaders: Object.assign({}, req.headers),
2467+
requestProtocol: protocol.substring(0, protocol.length - 1) as
2468+
| 'http'
2469+
| 'https',
2470+
})
24672471

24682472
// TODO: investigate, this is not safe across multiple concurrent requests
24692473
incrementalCache.resetRequestCache()

packages/next/src/server/web/adapter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,10 @@ export async function adapter(
186186
}
187187

188188
if (
189-
!(globalThis as any).__incrementalCache &&
189+
// If we are inside of the next start sandbox
190+
// leverage the shared instance if not we need
191+
// to create a fresh cache instance each time
192+
!(globalThis as any).__incrementalCacheShared &&
190193
(params as any).IncrementalCache
191194
) {
192195
;(globalThis as any).__incrementalCache = new (

packages/next/src/server/web/sandbox/sandbox.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export async function getRuntimeContext(
7777
})
7878

7979
if (params.incrementalCache) {
80+
runtime.context.globalThis.__incrementalCacheShared = true
8081
runtime.context.globalThis.__incrementalCache = params.incrementalCache
8182
}
8283

0 commit comments

Comments
 (0)