Skip to content

Commit

Permalink
fix(cache): only invalidate if validate returns false (#1796)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Oct 5, 2023
1 parent a03a18b commit 3f07e05
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/runtime/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function defineCachedFunction<T, ArgsT extends unknown[] = unknown[]>(
shouldInvalidateCache ||
entry.integrity !== integrity ||
(ttl && Date.now() - (entry.mtime || 0) > ttl) ||
!validate(entry);
validate(entry) === false;

const _resolve = async () => {
const isPending = pending[key];
Expand Down Expand Up @@ -112,7 +112,7 @@ export function defineCachedFunction<T, ArgsT extends unknown[] = unknown[]>(
entry.mtime = Date.now();
entry.integrity = integrity;
delete pending[key];
if (validate(entry)) {
if (validate(entry) !== false) {
const promise = useStorage()
.setItem(cacheKey, entry)
.catch((error) => {
Expand All @@ -132,7 +132,7 @@ export function defineCachedFunction<T, ArgsT extends unknown[] = unknown[]>(
event.waitUntil(_resolvePromise);
}

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

0 comments on commit 3f07e05

Please sign in to comment.