Skip to content

Commit

Permalink
remove swr removal logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 10, 2024
1 parent 4c644b4 commit d716113
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions src/runtime/internal/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,15 @@ export function defineCachedFunction<T, ArgsT extends unknown[] = any[]>(
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) {
Expand All @@ -137,18 +140,6 @@ export function defineCachedFunction<T, ArgsT extends unknown[] = any[]>(
_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;
}
Expand Down

0 comments on commit d716113

Please sign in to comment.