Commit dde3976d authored by Janka Uryga's avatar Janka Uryga

allow revalidate in cache handler, fix revalidate write

parent 66c194a5
...@@ -64,16 +64,18 @@ const DefaultCacheHandler: CacheHandler = { ...@@ -64,16 +64,18 @@ const DefaultCacheHandler: CacheHandler = {
} }
const entry = privateEntry.entry const entry = privateEntry.entry
if ( if (process.env.NODE_ENV !== 'development') {
performance.timeOrigin + performance.now() > if (
entry.timestamp + entry.revalidate * 1000 performance.timeOrigin + performance.now() >
) { entry.timestamp + entry.revalidate * 1000
// In-memory caches should expire after revalidate time because it is ) {
// unlikely that a new entry will be able to be used before it is dropped // In-memory caches should expire after revalidate time because it is
// from the cache. // unlikely that a new entry will be able to be used before it is dropped
debug?.('get', cacheKey, 'expired') // from the cache.
debug?.('get', cacheKey, 'expired')
return undefined
return undefined
}
} }
let revalidate = entry.revalidate let revalidate = entry.revalidate
......
...@@ -1508,43 +1508,65 @@ export function cache( ...@@ -1508,43 +1508,65 @@ export function cache(
// If this is stale, and we're not in a prerender (i.e. this is // 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 // dynamic render), then we should warm up the cache with a fresh
// revalidated entry. // revalidated entry.
const result = await generateCacheEntry( const revalidateAndWriteNewEntry = async () => {
workStore, const result = await generateCacheEntry(
// This is not running within the context of this unit. workStore,
{ kind: cacheContext.kind, outerWorkUnitStore: undefined }, // This is not running within the context of this unit.
clientReferenceManifest, { kind: cacheContext.kind, outerWorkUnitStore: undefined },
encodedCacheKeyParts, clientReferenceManifest,
fn, encodedCacheKeyParts,
sharedErrorStack fn,
) sharedErrorStack
)
if (result.type === 'cached') { if (result.type === 'cached') {
const { stream: ignoredStream, pendingCacheEntry } = result const { stream: ignoredStream, pendingCacheEntry } = result
let savedCacheEntry: Promise<CacheEntry> let savedCacheEntry: Promise<CacheEntry>
if (prerenderResumeDataCache) { // TODO: do we ever want to mutate the `prerenderResumeDataCache` while a render is ongoing?
const split = clonePendingCacheEntry(pendingCacheEntry) if (
savedCacheEntry = getNthCacheEntry(split, 0) prerenderResumeDataCache &&
prerenderResumeDataCache.cache.set( // This is not covered by a cache read, so in a dev staged render
serializedCacheKey, // we don't want to overwrite the existing cache entry
getNthCacheEntry(split, 1) // both to avoid timing issues (with cache entries ehtat aren't finished yet) and tearing.
) !(
} else { process.env.NODE_ENV === 'development' &&
savedCacheEntry = pendingCacheEntry workUnitStore &&
} // eslint-disable-next-line no-restricted-syntax
workUnitStore.type === 'request' &&
workUnitStore.stagedRendering
)
) {
const split = clonePendingCacheEntry(pendingCacheEntry)
savedCacheEntry = getNthCacheEntry(split, 0)
prerenderResumeDataCache.cache.set(
serializedCacheKey,
getNthCacheEntry(split, 1)
)
} else {
savedCacheEntry = pendingCacheEntry
}
if (cacheHandler) { let pendingWrite: Promise<void> | undefined
const promise = cacheHandler.set( if (cacheHandler) {
serializedCacheKey, pendingWrite = cacheHandler.set(
savedCacheEntry serializedCacheKey,
) savedCacheEntry
)
}
workStore.pendingRevalidateWrites ??= [] await ignoredStream.cancel()
workStore.pendingRevalidateWrites.push(promise) return pendingWrite
} }
await ignoredStream.cancel()
} }
// TODO: ideally, this'd be scheduled to run after the render is finished
// to avoid interfering with any critical work.
const pendingRevalidatePromise = new Promise((resolve) =>
setTimeout(resolve)
).then(() => revalidateAndWriteNewEntry())
workStore.pendingRevalidateWrites ??= []
workStore.pendingRevalidateWrites.push(pendingRevalidatePromise)
} }
} }
} }
......
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