swr
swr copied to clipboard
new feature for stale time
SWR would't send requests of same key anymore but return the cache of it within stale time
when stale timer expires, SWR would resend request of current page on mounted or when we mutate it manually
How is it going with this feature?
Still look forward to this one.
I'm surprised to see that there's (apparently?) no way to disable caching. I need to do this for testing / illustrative purposes. In particular I'm trying to simulate slow network fetches and make sure my UI components have appropriate loading states, etc. It's easy for me to simulate the slow network fetch, but useSWR
is defeating that after the first fetch by serving cached results.
Is there any option other than to manually remove cache entries as illustrated in https://github.com/vercel/swr/discussions/456#discussioncomment-986323 ?
For now append t=${Date.now()}
to request url will help to disable 304 cache 🤷
const url = `https://sample.com/greet?t=${Date.now()}`
This is my method, I don't know if it can help you.
const disableCache = (useSWRNext) => {
return (key, fetcher, config) => {
const swr = useSWRNext(key, fetcher, config);
const { data, isValidating } = swr;
return Object.assign({}, swr, {
data: isValidating ? undefined : data,
});
};
};
export default (params) =>
useSWR(URL, fetcher, {
focusThrottleInterval: 0,
use: [disableCache],
});
@fhyfhyfhy That looks pretty clever! I will give it a shot, thanks.