You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This happens when you clear the cache, as described at the end of: https://swr.vercel.app/docs/mutation#mutate-multiple-items
If a useSWR call was done shortly before that cache clear, then this call will be skipped if it happens shortly after the cache clear, and the returned data will be undefined.
This issue happens only when the delay between the first call and the second call is smaller than dedupingInterval (2 s by default).
Setting dedupingInterval to 0 works around the issue, but disabling dedupes is not always desirable.
Expected Behavior
All useSWR requests called for the first time since a cache clear must run and not be deduped.
The problem comes from the fact that the documentation calls this "clear all cache data":
mutate(()=>true,undefined,{revalidate: false})
when in practice it’s not exactly a cache clear, as it does not remove any cache key, but sets them to undefined instead.
The proper solution would probably be to have an official way of clearing the cache.
The SWR cache utilizes JavaScript's built-in Map object for data storage. To clear the entire cache, you can directly call the clear method on the cache object retrieved using useSWRConfig.
const { cache } = useSWRConfig();
cache.clear();
Important Note:
Currently, there are no official TypeScript definitions for the clear method on the cache object. However, a pull request addressing this issue can be found here: #2950
Bug report
Description / Observed Behavior
This happens when you clear the cache, as described at the end of: https://swr.vercel.app/docs/mutation#mutate-multiple-items
If a
useSWR
call was done shortly before that cache clear, then this call will be skipped if it happens shortly after the cache clear, and the returned data will beundefined
.This issue happens only when the delay between the first call and the second call is smaller than
dedupingInterval
(2 s by default).Setting
dedupingInterval
to 0 works around the issue, but disabling dedupes is not always desirable.Expected Behavior
All
useSWR
requests called for the first time since a cache clear must run and not be deduped.The problem comes from the fact that the documentation calls this "clear all cache data":
when in practice it’s not exactly a cache clear, as it does not remove any cache key, but sets them to
undefined
instead.The proper solution would probably be to have an official way of clearing the cache.
Repro Steps / Code Example
Full example: https://codesandbox.io/p/sandbox/useswr-deduped-after-cache-clear-qkrwmj
f2bc2152-b5f7-4eec-95af-e3f579c11dd5.07.10.mp4
Additional Context
SWR version 2.2.4
The text was updated successfully, but these errors were encountered: