-
-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
non-2xx (including 5xx) responses are being cached #341
Comments
Try const { cache } = useFetch()
cache.delete('cache-key') |
Thanks for the response @alex-cory. I'm not sure that I fully understand how to use Perhaps my codesandbox was a little too simple. I've updated the codesandbox to be a little closer to how I am using It is in the A trimmed down version of function useFetchMyResource() {
const { cache } = useFetch({ persist: true, cachePolicy: 'cache-first' });
return useFetch(
"/my-url",
{
persist: true,
cacheLife: 10 * 60 * 1000, // 10 minutes in ms
cachePolicy: 'cache-first',
interceptors: {
response: ({ response }) => {
if (!response.ok) {
cache.delete('/my-url');
}
return response;
},
},
},
[]);
} Thanks for your help. |
After more experimentation, I have something that kind of works. I've updated the codesandbox and I'll repeat the function useFetchMyResource() {
const request = useFetch(
url,
{
persist: true,
cacheLife: 10 * 60 * 1000, // 10 minutes in ms
cachePolicy: "cache-first"
},
[]
);
if (request.error) {
// Using `url` does not work.
// request.cache.delete(url);
// Need to reconstruct the cache key manually.
const cacheKey = `url:${url}||method:GET||body:`;
request.cache.delete(cacheKey)
}
return request;
} Unfortunately, getting this to work requires that the cache key is manually reconstructed. Explicitly removing the cache for bad responses would be OK if I had access to the response id. Would it be possible for you to expose the response id (#316)? I still think that |
Hm... does the normal cache-first caching strategy not cache |
@alex-cory any news on that one? I'm currently struggling with the same issues as @benarmston |
When
cachePolicy
is set tocache-first
, non-2xx
responses are cached. I want to be able to avoid caching non-2xx
response.The codesandbox uses a
404
response as that is what was easiest for me to use there. However, the actual issue I ran into was with a503
response. Caching5xx
responses is a bad idea.** Codesandbox **
https://codesandbox.io/s/kind-christian-x2he1?file=/src/App.js
To Reproduce
Steps to reproduce the behavior:
404
response was cached.Expected behavior
Either non-
2xx
responses do not get cached; or there should be an option so that only2xx
responses are cached.The text was updated successfully, but these errors were encountered: