Skip to content

SWR cached routes won't ever update content if response is empty or response is 404 #1992

Closed

Description

@mdarrik

Environment

##Reproduction env:

  • Stackblitz
  • Nitro v2.8.1
  • Node v18.18.0

Env where encountered

  • Operating System: Linux
  • Node Version: v18.17.1
  • Nuxt Version: 3.8.1
  • CLI Version: 3.9.1
  • Nitro Version: 2.7.2
  • Package Manager: yarn@1.22.19

Reproduction

https://stackblitz.com/edit/github-wlae5p?file=routes%2Findex.ts

  1. Start the site either with npm run dev or npm build && node .output/server/index.mjs
  2. Navigate to the add-value-to-storage endpoint
  3. Navigate to the swr endpoint (will see nitro: is-awesome)
  4. Navigate to the not-swr endpoint (will see nitro: is-awesome)
  5. Navigate to the remove-value-from-storage endpoint
  6. Navigate to the not-swr route and refresh until the cache expires
  7. Will eventually get a blank response
  8. Navigate to the swr endpoint
  9. It'll continue to return the nitro: is-awesome response indefinitely.

Describe the bug

Routes cached by SWR cannot be updated to return empty/404 values without fully clearing the cache.

Description

We currently have a high-traffic Nuxt 3 site that's backed by a CMS. We have the pages and a Nuxt/Nitro API set to cache with SWR for 60 minutes.

When content editors delete a page, our CMS API will return null, which is what we'd expect our Nitro API to return. However, if the nitro API returns null or manually returns a 404 response, Nitro never seems to invalidate the SWR cache. This means we can't delete pages without manually clearing entries from the cache. This feels unexpected since there might be valid reasons for an API to return a 404 or 204 response.

Additional context

No response

Logs

No response

Activity

  1. infabo commented on Dec 5, 2025

    @infabo

    Why is this issue stale? This is a real-world issue.

  2. pi0 commented on Mar 19, 2026

    @pi0
    Member

    This is now fixed in unjs/ocache (used by Nitro v3):

    • When SWR background revalidation throws (e.g. 404 error), the stale cache entry is evicted from storage
    • When SWR background revalidation returns a value that fails validation (e.g. empty/undefined), the stale cache entry is evicted from storage

    This prevents the "stale forever" behavior where deleted content keeps being served.

    See unjs/ocache#9

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions