Skip to content

Commit 4e3e831

Browse files
committed
fix(cache): respect staleMaxAge option
1 parent eaffb9a commit 4e3e831

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
@@ -131,7 +131,13 @@ export function defineCachedFunction<T, ArgsT extends unknown[] = any[]>(
131131
event.waitUntil(_resolvePromise);
132132
}
133133

134-
if (opts.swr && validate(entry) !== false) {
134+
const staleTtl = (opts.staleMaxAge ?? 0) * 1000;
135+
const staleExpired =
136+
shouldInvalidateCache ||
137+
entry.integrity !== integrity ||
138+
(staleTtl && Date.now() - (entry.mtime || 0) > staleTtl) ||
139+
validate(entry) === false;
140+
if (opts.swr && !staleExpired) {
135141
_resolvePromise.catch((error) => {
136142
console.error(`[nitro] [cache] SWR handler error.`, error);
137143
useNitroApp().captureError(error, { event, tags: ["cache"] });

0 commit comments

Comments
 (0)