-
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
cache.clear() not available even though docs imply that it is #1887
Comments
If you're using the default cache, which is a JS map, there is a clear method, it's not just on SWR's Cache type. But you can workaround this with:
Not pretty, but it will work until the type is fixed. |
Though, I would actually prefer cache.keys() to be exposed, because clearing the cache without a mutate might conceivably leave swr in a place where there's no data and swr doesn't know to refetch it, so i think:
might be slightly better. |
For some reason I couldn't get I even went so far as to wrap things in async code and afterEach(async () => {
const hook = renderHook(() => useSWRConfig())
await act(() => {
const cache = hook.result.current.cache as Map<string, any>
await cache.clear()
})
}) From the code, it definitely looks like Iterating This has been a huge frustration for the past few weeks and I've tried many different hoops to write reasonable tests. Currently I've landed on a solution that wraps // global storage
const cache: Set<string> = new Set()
export function useSWRPlus(key, ...) {
const swr = useSWR(() => {
// need to always use the fn method of key to do the processing, so something like
const normalizedKey = typeof key === 'function' ? key() : key
if (normalizedKey) {
cache.add(JSON.stringify(normalizedKey))
return normalizedKey
},
...
)
// Unfortunately it always needs this now even though it'll almost never be used
const swrConfig = useSWRConfig()
return {
...swr,
async clear() {
const keys = cache.keys()
for (const key of keys) {
const parsedKey = JSON.parse(key)
// You could add some conditional here to only match certain keys, maybe that's part
// of your swr wrapper or maybe your clear method can optionally take some matcher
await swrConfig.mutate(parsedKey, undefined)
cache.delete(key)
}
}
}
} Probably better would be to move the global |
We released a new API change latest beta v2.0.0-beta.5 that could let you do the following code to remove all cache data: mutate(/* match all keys */() => true, undefined, false) Notice that the functional key is replaced by a key filter function. That's the better way than |
This API seems quite awkward. Try to give this code to somebody who haven't read the docs and tell them to guess what it does and what each param does. mutate(() => true, undefined, false) What's preventing us from having a |
The v2 docs are still in progress, the more details about the mutate API change are in PR #1989 or RFC #1946 The new mutate API will look like below which can take either a function mutate(keyFilter | key, data, revalidate?) The idea was to leverage the existing We'll give more examples about that change in v2 docs later. |
We still need a function to clear the entire cache contents
I have seen several bugs closed here recently with a reference to the fix introduced in 1.2.2 where you can now call cache.clear(key)
HOWEVER this still does match the documentation, which asserts that there is a cache.clear() function that can be used to clear the entire contents of the cache. Typescript complains that there is no such function.
Bottom line: is there any way to clear the entire contents of the cache?
The text was updated successfully, but these errors were encountered: