vercel / swr

React Hooks for Data Fetching
https://swr.vercel.app
MIT License
30.24k stars 1.21k forks source link

Global mutate does not invalidate `useSWRInfinite` requests #3015

Open colemars opened 1 week ago

colemars commented 1 week ago

Bug report

Description / Observed Behavior

I have a series of pages fetched via useSWRInfinite. I am attempting to invalidate them via the global mutate function. In testing, I did simply

mutate((k) => {
        console.log('k', k)
        return true
      })

to observe all cache keys and, theoretically, trigger invalidations for all of them as well (again, for testing).

However, despite seeing the relevant cache keys logged - the requests from useSWRInfinite did not revalidate.

Expected Behavior

If I run

mutate((k: any) => {
        console.log('k', k)
        return true
      })

I would expect all cached requests to revalidate, as described in the docs.

Repro Steps / Code Example

I took the example from the docs and just replaced mutate with the above function using global mutate.

https://codesandbox.io/p/sandbox/swr-infinite-forked-h7y9zw?file=%2Fsrc%2FApp.js%3A68%2C14

Additional Context

^2.2.5 Add any other context about the problem here.

colemars commented 1 week ago
    for (const key of it) {
      if (
        // Skip the special useSWRInfinite and useSWRSubscription keys.
        !/^\$(inf|sub)\$/.test(key) &&
        keyFilter((cache.get(key) as { _k: Arguments })._k)
      ) {
        matchedKeys.push(key)
      }
    }

Would y'all be open to adding a way to access the special keys in the global mutator? It seems not desirable that infinite keyed requests cannot be globally mutated via partial matching with the filter method.