Unverified Commit e733853c authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Ensure request specific caches for revalidate are reset (#60810)

This ensures our in memory cache leveraged for the `fetch-cache`
interface is reset per-request instead of using an arbitrary timeout so
that we are always pulling the revalidate state from upstream correctly
on new requests.

Before:
https://vercel-revalidate-3zi8wpexo-vtest314-ijjk-testing.vercel.app/
After:
https://vercel-revalidate-70y5a8dbh-vtest314-ijjk-testing.vercel.app/

Closes: https://github.com/vercel/next.js/issues/60680

Closes NEXT-2144
parent ecbc36cd
...@@ -1271,6 +1271,7 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -1271,6 +1271,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {
| 'http' | 'http'
| 'https', | 'https',
}) })
incrementalCache.resetRequestCache()
addRequestMeta(req, 'incrementalCache', incrementalCache) addRequestMeta(req, 'incrementalCache', incrementalCache)
;(globalThis as any).__incrementalCache = incrementalCache ;(globalThis as any).__incrementalCache = incrementalCache
} }
...@@ -2141,6 +2142,8 @@ export default abstract class Server<ServerOptions extends Options = Options> { ...@@ -2141,6 +2142,8 @@ export default abstract class Server<ServerOptions extends Options = Options> {
| 'https', | 'https',
})) }))
incrementalCache?.resetRequestCache()
const { routeModule } = components const { routeModule } = components
type Renderer = (context: { type Renderer = (context: {
......
...@@ -106,6 +106,10 @@ export default class FetchCache implements CacheHandler { ...@@ -106,6 +106,10 @@ export default class FetchCache implements CacheHandler {
} }
} }
public resetRequestCache(): void {
memoryCache?.reset()
}
public async revalidateTag(tag: string) { public async revalidateTag(tag: string) {
if (this.debug) { if (this.debug) {
console.log('revalidateTag', tag) console.log('revalidateTag', tag)
...@@ -157,14 +161,11 @@ export default class FetchCache implements CacheHandler { ...@@ -157,14 +161,11 @@ export default class FetchCache implements CacheHandler {
return null return null
} }
// memory cache is cleared at the end of each request
// so that revalidate events are pulled from upstream
// on successive requests
let data = memoryCache?.get(key) let data = memoryCache?.get(key)
// memory cache data is only leveraged for up to 2 seconds
// so that revalidation events can be pulled from source
if (Date.now() - (data?.lastModified || 0) > 2000) {
data = undefined
}
// get data from fetch cache // get data from fetch cache
if (!data && this.cacheEndpoint) { if (!data && this.cacheEndpoint) {
try { try {
......
...@@ -91,6 +91,8 @@ export default class FileSystemCache implements CacheHandler { ...@@ -91,6 +91,8 @@ export default class FileSystemCache implements CacheHandler {
} }
} }
public resetRequestCache(): void {}
private loadTagsManifest() { private loadTagsManifest() {
if (!this.tagsManifestPath || !this.fs || tagsManifest) return if (!this.tagsManifestPath || !this.fs || tagsManifest) return
try { try {
......
...@@ -60,6 +60,8 @@ export class CacheHandler { ...@@ -60,6 +60,8 @@ export class CacheHandler {
): Promise<void> {} ): Promise<void> {}
public async revalidateTag(_tag: string): Promise<void> {} public async revalidateTag(_tag: string): Promise<void> {}
public resetRequestCache(): void {}
} }
export class IncrementalCache implements IncrementalCacheType { export class IncrementalCache implements IncrementalCacheType {
...@@ -213,6 +215,10 @@ export class IncrementalCache implements IncrementalCacheType { ...@@ -213,6 +215,10 @@ export class IncrementalCache implements IncrementalCacheType {
return fetchCache ? pathname : normalizePagePath(pathname) return fetchCache ? pathname : normalizePagePath(pathname)
} }
resetRequestCache() {
this.cacheHandler?.resetRequestCache?.()
}
async unlock(cacheKey: string) { async unlock(cacheKey: string) {
const unlock = this.unlocks.get(cacheKey) const unlock = this.unlocks.get(cacheKey)
if (unlock) { if (unlock) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment