getCachedData Not Working
Hi could some give me help getCachedData stopped working and i also disabled purgeCachedData and still doesnt work
2 Replies
Hi @GamerPedro4K. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
It looks like you're using a custom
Singleton Data Fetching Layer
purgeCachedData and granularCachedData ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
getCachedData
function with useFetch
, but it's not working as expected—even after disabling purgeCachedData
. Here are some key points and troubleshooting steps based on the latest Nuxt documentation and recent changes:
1. getCachedData
Context and Usage
- In recent Nuxt versions, the getCachedData
function receives a third argument: a context object (ctx
) that includes the cause of the request (e.g., 'initial'
, 'refresh:manual'
, etc.). If you don't handle this, your cache might not work as intended, especially on manual refreshes or reactivity triggers.
- Example from the docs:
This ensures that on a manual refresh, the cache is bypassed and fresh data is fetched. If you omit the ctx
parameter, your function may not be called correctly in all scenarios Singleton Data Fetching Layer.
2. granularCachedData
Experimental Flag
- By default, Nuxt only uses getCachedData
on the initial fetch. To ensure it is called on every refresh (manual, watch, etc.), you must enable the granularCachedData
experimental feature in your nuxt.config.ts
:
purgeCachedData and granularCachedData
3. Option Consistency
- If you use the same key for multiple useFetch
calls, all options (including getCachedData
) must be consistent. Otherwise, Nuxt will warn or behave unexpectedly Singleton Data Fetching Layer.
4. Disabling purgeCachedData
- Disabling purgeCachedData
only prevents Nuxt from automatically clearing cached data. It does not affect when or how getCachedData
is called purgeCachedData.
5. Example Fix
Update your getCachedData
to accept the third ctx
argument, and enable granularCachedData
:
Summary
- Make sure your getCachedData
function uses the correct signature (with ctx
).
- Enable granularCachedData
in your config.
- Ensure all options are consistent for the same key.
If you follow these steps and it still doesn't work, there may be a bug or regression in your Nuxt version. In that case, check for related issues or updates in the Nuxt repository.
References:Singleton Data Fetching Layer
purgeCachedData and granularCachedData ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
<@537544951005839376> Kapa.ai is still learning and improving, please let me know how I did by reacting below.