swr icon indicating copy to clipboard operation
swr copied to clipboard

new feature for stale time

Open lxjwlt opened this issue 2 years ago • 1 comments

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

lxjwlt avatar Dec 13 '21 07:12 lxjwlt

How is it going with this feature?

SunStupic avatar Mar 11 '22 08:03 SunStupic

Still look forward to this one.

n3ih7 avatar Oct 05 '22 02:10 n3ih7

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 ?

mikelehen avatar Nov 09 '22 22:11 mikelehen

For now append t=${Date.now()} to request url will help to disable 304 cache 🤷

const url = `https://sample.com/greet?t=${Date.now()}`

AielloChan avatar Nov 11 '22 13:11 AielloChan

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 avatar Nov 14 '22 03:11 fhyfhyfhy

@fhyfhyfhy That looks pretty clever! I will give it a shot, thanks.

mikelehen avatar Nov 14 '22 05:11 mikelehen