-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useSWR deduped by mistake after cache clear #2877
Comments
Ran into same issue. In my case, user logs out, clear cache (per documentation), and logs in before the dedup interval, gets no data. |
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.
Important Note: |
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: