Skip to content
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

Open
BertrandBordage opened this issue Jan 21, 2024 · 2 comments
Open

useSWR deduped by mistake after cache clear #2877

BertrandBordage opened this issue Jan 21, 2024 · 2 comments

Comments

@BertrandBordage
Copy link

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 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.

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

@wavded
Copy link

wavded commented Feb 9, 2024

Ran into same issue. In my case, user logs out, clear cache (per documentation), and logs in before the dedup interval, gets no data.

@shhonarmandi
Copy link

shhonarmandi commented Apr 27, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants