Skip to content

Commit

Permalink
fix(cache): only return validated stale value (#1795)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Oct 5, 2023
1 parent 0599ace commit a03a18b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/runtime/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function defineCachedFunction<T, ArgsT extends unknown[] = unknown[]>(
const group = opts.group || "nitro/functions";
const name = opts.name || fn.name || "_";
const integrity = opts.integrity || hash([fn, opts]);
const validate = opts.validate || (() => true);
const validate = opts.validate || ((entry) => entry.value !== undefined);

async function get(
key: string,
Expand Down Expand Up @@ -132,7 +132,7 @@ export function defineCachedFunction<T, ArgsT extends unknown[] = unknown[]>(
event.waitUntil(_resolvePromise);
}

if (opts.swr && entry.value) {
if (opts.swr && validate(entry)) {
_resolvePromise.catch((error) => {
console.error(`[nitro] [cache] SWR handler error.`, error);
useNitroApp().captureError(error, { event, tags: ["cache"] });
Expand Down

0 comments on commit a03a18b

Please sign in to comment.