SWR 0.3.8
You should prevent automatic revalidation which is controlled by the next options:
{
revalidateOnFocus: false,
revalidateOnMount:false,
revalidateOnReconnect: false,
refreshWhenOffline: false,
refreshWhenHidden: false,
refreshInterval: 0,
}
According to the documentation refreshWhenOffline
, refreshWhenHidden
and refreshInterval
are disabled by default, so it's enough only to set:
{
revalidateOnFocus: false,
revalidateOnMount:false,
revalidateOnReconnect: false,
}
This will restrict SWR from running the fetching method. However, you still need the first one run to get your data.
You may end up with something like this:
const useStaleSWR = (dataKey) => {
const revalidationOptions = {
revalidateOnMount: !cache.has(dataKey), //here we refer to the SWR cache
revalidateOnFocus: false,
revalidateOnReconnect: false,
};
return useSWR(dataKey, fetcher, revalidationOptions);
}
SWR cache is exposed since 0.2.0 version
After all, I do not recommend using SWR for this purpose. SWR built to fetch data again. If you have static data - fetch once and store in session/local storage or define your own cache.