You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const AxiosInstance = axios.create({ /* headers */ });
const AxiosCacheInstance = axios.create({ /* same headers */ });
// Add interceptors to BOTH
AxiosInstance.interceptors.request.use(...);
AxiosInstance.interceptors.response.use(...);
AxiosCacheInstance.interceptors.request.use(...);
AxiosCacheInstance.interceptors.response.use(...);
// Now only cache AxiosCacheInstance
export const CacheAxiosInstance = setupCache(AxiosCacheInstance, {
methods: ['get', 'post'],
interpretHeader: false,
storage: asyncStorageCache,
debug: console.log,
});
export default AxiosInstance;
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:
On the first API call, check if cached data exists and return it immediately to update the UI.
In the background, fetch updated data from the server and refresh the UI with this new data.
Replace the old cached response with the newly fetched response.
One more question — do I need to create a new instance for caching APIs?
This discussion was converted from issue #1002 on April 22, 2025 08:48.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
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?
Beta Was this translation helpful? Give feedback.
All reactions