Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cache): only invalidate if validate returns false #1796

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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