Skip to content

Commit

Permalink
Remove shouldRevalidateStale concept from CacheHandlers
Browse files Browse the repository at this point in the history
We want to immediately expire any stale entries from memory caches. So we
just have treat them as missing/expired directly.
  • Loading branch information
sebmarkbage committed Sep 26, 2024
1 parent c13f502 commit 6dc1336
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions packages/next/src/server/use-cache/use-cache-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ import { staticGenerationAsyncStorage } from '../../client/components/static-gen

type CacheEntry = {
value: ReadableStream
// In-memory caches are fragile and should not use stale-while-revalidate
// semantics on the caches because it's not worth warming up an entry that's
// likely going to get evicted before we get to use it anyway. However,
// we also don't want to reuse a stale entry for too long so stale entries
// should be considered expired/missing in such CacheHandlers.
stale: boolean
}

interface CacheHandler {
get(cacheKey: string | ArrayBuffer): Promise<undefined | CacheEntry>
set(cacheKey: string | ArrayBuffer, value: ReadableStream): Promise<void>
shouldRevalidateStale: boolean
}

const cacheHandlerMap: Map<string, CacheHandler> = new Map()
Expand Down Expand Up @@ -57,10 +61,6 @@ cacheHandlerMap.set('default', {
await value.cancel()
}
},
// In-memory caches are fragile and should not use stale-while-revalidate
// semantics on the caches because it's not worth warming up an entry that's
// likely going to get evicted before we get to use it anyway.
shouldRevalidateStale: false,
})

const serverManifest: any = null // TODO
Expand Down Expand Up @@ -187,7 +187,7 @@ export function cache(kind: string, id: string, fn: any) {
let stream
if (
entry === undefined ||
(staticGenerationStore.isStaticGeneration && entry.stale)
(entry.stale && staticGenerationStore.isStaticGeneration)
) {
// Miss. Generate a new result.

Expand All @@ -210,11 +210,9 @@ export function cache(kind: string, id: string, fn: any) {
)
} else {
stream = entry.value
if (entry.stale && cacheHandler.shouldRevalidateStale) {
if (entry.stale) {
// If this is stale, and we're not in a prerender (i.e. this is dynamic render),
// then we should warm up the cache with a fresh revalidated entry. We only do this
// for long lived cache handlers because it's not worth warming up the cache with an
// an entry that's just going to get evicted before we can use it anyway.
// then we should warm up the cache with a fresh revalidated entry.
const ignoredStream = await runInCleanSnapshot(
generateCacheEntry,
staticGenerationStore,
Expand Down

0 comments on commit 6dc1336

Please sign in to comment.