Nitro cache not expiring on error
If a cached nitro handler throws an error, will the error prevent the cached result from being purged from cache even if the age of the cached result is way beyond its config? Is that intended behavior? I have a route that shows an outdated cached result and I think it's because now the api route throws a 404 in the handler because the underlying data is no longer available. But the nuxt API route does not reflect this, instead still showing the old and outdated cached result. This seems unintuitive to me?
2 Replies
Hi @Tackleberry. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the knowledge sources, there appears to be a known issue with cache invalidation in Nitro, which is the server engine used by Nuxt.
When a cached Nitro handler throws an error, it seems that the error might indeed prevent the cached result from being properly purged, even if the cache should have expired based on its configuration. This behavior has been reported as problematic in production environments.
There's a specific GitHub issue (#31030) that discusses cache invalidation not working properly on production environments like Vercel or Netlify, which was later tracked in the Nitro repository as issue #3105. This suggests that cache invalidation issues are recognized problems in the Nitro ecosystem Github Issue (closed): cache invalidation is not working on production.
Additionally, there was a specific fix in Nuxt 3.8.1 related to caching behavior with error responses from useFetch, as mentioned in a Stack Overflow post How can I disable the cache after receiving an error response from useFetch in Nuxt 3.8. This indicates that error handling with cached responses has been problematic enough to warrant specific fixes.
For your specific situation where you're seeing outdated cached results despite errors, you might want to try:
1. Using
clearNuxtData()
to manually invalidate the cache for specific keys
2. Adding a unique key
parameter to your fetch calls to prevent caching
3. Setting shorter cache durations or implementing a custom cache invalidation strategy
If you're working with a recent version of Nuxt, you might also want to check if the fixes from Nuxt 3.8.1 address your specific issue.
I hope this helps clarify the situation. The behavior you're experiencing does seem unintuitive and appears to be a recognized issue in certain scenarios.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@615642942777851938> Kapa.ai is still learning and improving, please let me know how I did by reacting below.