Repository navigation
Cache Interceptor Caching All APIs and Not Returning Fresh Data #1003
I am using a cache interceptor to cache specific URLs in my application. However, when I implement it like above code, it is caching all the APIs in the app, not just the ones I intend to cache. Also, it always returns the cached data, even when there is an active internet connection and new data is available from the API. I have tried multiple approaches (even consulting AI tools), but I haven't been able to solve it. Could someone please help me with this? Here is the exact behavior I want to achieve:
One more question — do I need to create a new instance for caching APIs? |
Replies: 1 comment
|
Disable caching globally and enable it only for selected requests: const api = setupCache(axios.create({ /* headers */ }), {
enabled: false
});
const response = await api.get("/cached-endpoint", {
cache: {
enabled: true,
override: true,
hydrate(cache) {
render(cache.data); // existing cached payload
}
}
});
render(response.data); // fresh network payload
This is not a detached background refresh: your code receives the old value through Also avoid adding Docs: |
setupCache()only affects the Axios instance passed to it, but caching is enabled by default for every eligible request made through that instance. You do not need a second instance just to use opt-in caching.Disable caching globally and enable it only for selected requests:
override: truemakes a network request even while the stored response is fresh.hydrateprovides the…