Skip to content

Commit 4c04aa4

Browse files
committed
fix(cache): respect staleMaxAge option
1 parent adcd395 commit 4c04aa4

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/runtime/internal/cache.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,13 @@ export function defineCachedFunction<T, ArgsT extends unknown[] = any[]>(
145145
event.waitUntil(_resolvePromise);
146146
}
147147

148-
if (opts.swr && validate(entry) !== false) {
148+
const staleTtl = (opts.staleMaxAge ?? 0) * 1000;
149+
const staleExpired =
150+
shouldInvalidateCache ||
151+
entry.integrity !== integrity ||
152+
(staleTtl && Date.now() - (entry.mtime || 0) > staleTtl) ||
153+
validate(entry) === false;
154+
if (opts.swr && !staleExpired) {
149155
_resolvePromise.catch((error) => {
150156
console.error(`[cache] SWR handler error.`, error);
151157
useNitroApp().captureError(error, { event, tags: ["cache"] });

0 commit comments

Comments
 (0)