-
-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possibility to opt-in cache #511
Comments
Hey @panghaoyuan, disabling cache by default seems to be a no-op to this package ship as a feature. If the hassle is worth, you can use To incrementally adopt axios-cache-interceptor in your current system, I'd export two axios instances in your code, like Feel free to reopen this issue :) |
You could also try axios defaults perhaps. import axios from "axios";
axios.defaults.cache = false; |
Just to leave a comment for anyone trying to achieve that and reaching here ... : Adding That's what I've done to "activate" the cache to only 1 route :
And in whateverServiceYouWouldLike.js :
And seems to be working ;) |
An even simpler solution:
|
According to the source code, I am currently using the following code to completely disable the default cache in my project import axios from 'axios'
import { setupCache, defaultRequestInterceptor, AxiosCacheInstance } from 'axios-cache-interceptor'
const getCacheRequestInterceptor = (axios: AxiosCacheInstance) => {
const defaultRequestInterceptorResult = defaultRequestInterceptor(axios)
const onFulfilled = ((config, ...args) => {
if (!config?.cache || (config?.cache as any)?.ttl <= 0) {
config.cache = false
}
return defaultRequestInterceptorResult.onFulfilled(config, ...args)
}) as typeof defaultRequestInterceptorResult.onFulfilled
const apply = (() => axios.interceptors.request.use(onFulfilled)) as typeof defaultRequestInterceptorResult.apply
return {
...defaultRequestInterceptorResult,
onFulfilled,
apply,
}
}
const rawAxiosInstance = axios.create()
const axiosInstance = setupCache(rawAxiosInstance, {
ttl: -1,
requestInterceptor: getCacheRequestInterceptor(rawAxiosInstance as AxiosCacheInstance),
})
export default axiosInstance |
Can I add the default cache disable feature to this project? This is very necessary for projects that want to manually use cache Like this import Axios from 'axios';
import { setupCache } from 'axios-cache-interceptor';
const instance = Axios.create();
const axios = setupCache(instance, {
cache: false,
}); |
This may cause issues in your application if you have multiple requests running simultaneously with different cache configurations. |
@arthurfiorette This is a needed feature since we don't want to have two axios instances in our applications. Disabling cache globally would be useful. |
What's the problem with 2 axios instances? |
@sayhicoelho You can try setting Although, I think having another axios instance for them would be ideal. |
Nice! I didn't think of that before. This is probably the best solution since once |
@arthurfiorette I need exectly the same feature: disabled cache by default but with options specified. |
It seems that a lot of people are wanting this feature, I'll reopen this issue in case someone wants to work on it. remember to add unit tests :) |
I think adding multiple instances of axios with distinct caching settings might be a possible workaround here. I don’t encounter any errors from running setupCache multiple times as long as I pass a different axios instance each time and this allows caching to be enabled only where needed, by using the cached instance. Additionally, extracting other global logic into a function that accepts an axios instance simplifies applying other interceptors consistently across all instances. Pseudo-code:import { setupCache } from 'axios-cache-interceptor'
// Function to add authentication interceptors
function addAuthToAxiosInstance(instance) {
instance.interceptors.request.use(config => { /* add token */ });
instance.interceptors.response.use(response => response, error => { /* handle auth errors */ });
}
// Create axios instances with different caching settings
const globalAxios = axios.create({})
const inMemoryCachedAxios = setupCache(axios.create(), { /* in-memory cache config */ })
const persistentCachedAxios = setupCache(axios.create(), { /* persistent cache config */ })
// Apply auth logic to each instance
addAuthToAxiosInstance(globalAxios)
addAuthToAxiosInstance(inMemoryCachedAxios)
addAuthToAxiosInstance(persistentCachedAxios)
export { globalAxios, inMemoryCachedAxios, persistentCachedAxios } This setup allows selective caching with minimal code changes and keeps the configuration for each API call flexible. |
I have an old project of
nextjs
project.. Most of the pages of the project do not need caching. I only need to use this cache on a few pages. How to set the global setting to disable caching, I only need to enable caching on a few pages.such as
or
how can i resolve this question
The text was updated successfully, but these errors were encountered: