You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
support staleMaxAge handling for cache storage #2164
Configure routeRules to enable swr and set a stale max age.
e.g.
{
swr: true,
staleMaxAge: 10*60 // 10 minutes
}
Describe the bug
Nitro does not consider staleMaxAge and validate the age of a cached value when swr is enabled. It will always return a stale value if one is present, even if it is older than than the configured stale period.
It has a single route with a max age of 10 seconds and stale max age of 30 seconds.
On the initial request, the handler should be executed and the result returned. This works.
If refreshing within 10 seconds, the handler should not be executed and instead the previous result returned. This also works.
If refreshing 10 seconds after the initial request, but before 30 seconds, the cached result should still be used, but the handler should be executed in the background. This works too.
If refreshing more than 30 seconds after the initial request (or last update), the stale period has expired and the cached result should not be used. This is where the bug occurs.
🤖 AI-assisted comment: This comment was drafted by an AI agent (Claude) on behalf of the maintainer, based on an automated audit of ocache's source. Please verify before relying on it.
This is now fixed in nitro v3, which uses the standalone ocache package for its caching layer (defineCachedFunction/defineCachedHandler). ocache's src/cache.ts includes an isFullyExpired check that compares Date.now() - mtime against maxAge + staleMaxAge: once an entry is past its full stale window, the cached value is cleared entirely rather than served, forcing a fresh synchronous recompute instead of serving stale forever. So staleMaxAge is now properly bounded and respected end-to-end, both for the freshness check on read and for the underlying storage TTL.
Activity
cjpearson commentedon Jul 10, 2024
I've created a basic reproduction here.
It has a single route with a max age of 10 seconds and stale max age of 30 seconds.
On the initial request, the handler should be executed and the result returned. This works.
If refreshing within 10 seconds, the handler should not be executed and instead the previous result returned. This also works.
If refreshing 10 seconds after the initial request, but before 30 seconds, the cached result should still be used, but the handler should be executed in the background. This works too.
If refreshing more than 30 seconds after the initial request (or last update), the stale period has expired and the cached result should not be used. This is where the bug occurs.
ronenteva commentedon Sep 5, 2024
Sorry to bump, but I think this is a major issue that deserves more attention.
Seems like it's already solved, just wasn't merged for some reason.
[-]staleMaxAge is not respected when swr is enabled[/-][+]support `staleMaxAge` handling for cache storage[/+]qlash commentedon Apr 25, 2025
pi0x commentedon Jul 8, 2026
This is now fixed in nitro v3, which uses the standalone
ocachepackage for its caching layer (defineCachedFunction/defineCachedHandler). ocache'ssrc/cache.tsincludes anisFullyExpiredcheck that comparesDate.now() - mtimeagainstmaxAge + staleMaxAge: once an entry is past its full stale window, the cached value is cleared entirely rather than served, forcing a fresh synchronous recompute instead of serving stale forever. SostaleMaxAgeis now properly bounded and respected end-to-end, both for the freshness check on read and for the underlying storage TTL.