Skip to content

support staleMaxAge handling for cache storage #2164

Closed

Description

@cjpearson

Environment

node v20.11.1, Nuxt 3.10.2, Nitro 2.8.1

Reproduction

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.

Additional context

It looks like here only the swr flag is checked and the age of the entry and staleMaxAge is not considered. https://github.com/unjs/nitro/blob/e9975ac36914f582d7c7b724945e0abdd398291a/src/runtime/cache.ts#L141

Logs

No response

Activity

  1. added
    pending triage
    on Feb 23, 2024
  2. added
    needs reproduction
    on Feb 23, 2024
  3. cjpearson commented on Jul 10, 2024

    @cjpearson
    ContributorAuthor

    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.

  4. ronenteva commented on Sep 5, 2024

    @ronenteva

    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.

  5. changed the title [-]staleMaxAge is not respected when swr is enabled[/-] [+]support `staleMaxAge` handling for cache storage[/+] on Nov 8, 2024
  6. added
    enhancement
    cache
    and removed
    needs reproduction
    pending triage
    on Nov 8, 2024
  7. qlash commented on Apr 25, 2025

    @qlash
  8. pi0x commented on Jul 8, 2026

    @pi0x
    Contributor

    🤖 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cacheenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions