diff --git a/src/runtime/internal/cache.ts b/src/runtime/internal/cache.ts index 75386ac7af..5428809877 100644 --- a/src/runtime/internal/cache.ts +++ b/src/runtime/internal/cache.ts @@ -111,12 +111,15 @@ export function defineCachedFunction( entry.mtime = Date.now(); entry.integrity = integrity; delete pending[key]; - const promise = - validate(entry) === false - ? useStorage().removeItem(cacheKey) - : useStorage().setItem(cacheKey, entry); + const isValid = validate(entry) !== false; + const promise = isValid + ? useStorage().setItem(cacheKey, entry) + : useStorage().removeItem(cacheKey); promise.catch((error) => { - console.error(`[nitro] [cache] Cache update error.`, error); + console.error( + `[nitro] [cache] Cache ${isValid ? "update" : "removal"} error.`, + error + ); useNitroApp().captureError(error, { event, tags: ["cache"] }); }); if (event?.waitUntil) { @@ -137,18 +140,6 @@ export function defineCachedFunction( _resolvePromise.catch((error) => { console.error(`[nitro] [cache] SWR handler error.`, error); useNitroApp().captureError(error, { event, tags: ["cache"] }); - - // SWR revalidation failed, remove existing entry so we do - // not continue to return the cached value indefinitely. - const promise = useStorage() - .removeItem(cacheKey) - .catch((error) => { - console.error(`[nitro] [cache] Cache write error.`, error); - useNitroApp().captureError(error, { event, tags: ["cache"] }); - }); - if (event?.waitUntil) { - event.waitUntil(promise); - } }); return entry; }