Skip to content

Commit ec9ee87

Browse files
authored
Only share incremental cache for edge in next start (#79389)
This backports #79386 to 14 release channel
1 parent e65628a commit ec9ee87

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
@@ -1245,6 +1245,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
12451245
})
12461246
incrementalCache.resetRequestCache()
12471247
addRequestMeta(req, 'incrementalCache', incrementalCache)
1248+
// This is needed for pages router to leverage unstable_cache
1249+
// TODO: re-work this handling to not use global and use a AsyncStore
12481250
;(globalThis as any).__incrementalCache = incrementalCache
12491251
}
12501252

@@ -2106,13 +2108,15 @@ export default abstract class Server<ServerOptions extends Options = Options> {
21062108

21072109
// use existing incrementalCache instance if available
21082110
const incrementalCache =
2109-
(globalThis as any).__incrementalCache ||
2110-
(await this.getIncrementalCache({
2111-
requestHeaders: Object.assign({}, req.headers),
2112-
requestProtocol: protocol.substring(0, protocol.length - 1) as
2113-
| 'http'
2114-
| 'https',
2115-
}))
2111+
process.env.NEXT_RUNTIME === 'edge' &&
2112+
(globalThis as any).__incrementalCache
2113+
? (globalThis as any).__incrementalCache
2114+
: await this.getIncrementalCache({
2115+
requestHeaders: Object.assign({}, req.headers),
2116+
requestProtocol: protocol.substring(0, protocol.length - 1) as
2117+
| 'http'
2118+
| 'https',
2119+
})
21162120

21172121
incrementalCache?.resetRequestCache()
21182122

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

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

171171
if (
172-
!(globalThis as any).__incrementalCache &&
172+
// If we are inside of the next start sandbox
173+
// leverage the shared instance if not we need
174+
// to create a fresh cache instance each time
175+
!(globalThis as any).__incrementalCacheShared &&
173176
(params as any).IncrementalCache
174177
) {
175178
;(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
@@ -72,6 +72,7 @@ export async function getRuntimeContext(params: {
7272
})
7373

7474
if (params.incrementalCache) {
75+
runtime.context.globalThis.__incrementalCacheShared = true
7576
runtime.context.globalThis.__incrementalCache = params.incrementalCache
7677
}
7778

0 commit comments

Comments
 (0)