-
Hi, I assume when a SWR call is made with same key it uses data from cache. |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 12 replies
-
It's not possible, the idea of SWR (as the name implies) is to first return you stale data and the revalidate it against the API to get the new data. However, you can pass an array as key, if your fetch receives the URL as first argument and does nothing with the second argument you could do const random = React.useRef(Date.now())
const { data } = useSWR([url, random], fetcher) That will create a new |
Beta Was this translation helpful? Give feedback.
-
I've been testing a proof-of-concept stream based (p)react renderer with suspense support and I would be interested in providing no cache or forcing invalidation with the stateless environment. Since most server renderers don't support state it would be interesting to provide a custom cache mechanism for the server where state is not available. This would be essential once the offical react-dom server renderer supports suspense. |
Beta Was this translation helpful? Give feedback.
-
if someone needs a workaround for this in the future, mine is to clear the cache before calling import useSWR, { cache, ConfigInterface, fetcherFn, keyInterface, responseInterface } from 'swr';
import { useRef } from 'react';
export type SwrOptions<Data, E> = ConfigInterface<Data, E> & {
discardStale?: boolean,
};
export function useSwrWrapper<Data = any, Error = any>(key: keyInterface, fn?: fetcherFn<Data>, config?: SwrOptions<Data, Error>): responseInterface<Data, Error> {
const discardedStale = useRef(false);
if (key && config?.discardStale && !discardedStale.current) {
discardedStale.current = true;
cache.delete(key);
}
return useSWR(key, fn, config);
} IMO, it would be much better to be able to tell this specific hook not to return cache data for the first render, or to know whether the data is stale, but this works. |
Beta Was this translation helpful? Give feedback.
-
+1 In react-query, this can be achieved with |
Beta Was this translation helpful? Give feedback.
-
the workaround with random key using |
Beta Was this translation helpful? Give feedback.
-
new PR(arguable suggestion) for this issue with |
Beta Was this translation helpful? Give feedback.
-
We're also facing this issue, and was looking to go down the ref route. However we're using React Native and caching the data via our our driver (as per docs), but my concern is if we're adding a new item to local storage every time the component mounts, does that mean we're going to end up with thousands of entries in local storage over time? Or will SWR automatically clear out old or expired items? |
Beta Was this translation helpful? Give feedback.
-
I found a work around by using spaces. // First endpoint // Second endpoint |
Beta Was this translation helpful? Give feedback.
It's not possible, the idea of SWR (as the name implies) is to first return you stale data and the revalidate it against the API to get the new data.
However, you can pass an array as key, if your fetch receives the URL as first argument and does nothing with the second argument you could do
That will create a new
random
per mount (not per render) creating effectively a new key in the cache. But I don't recommend you to avoid the cache.