nandorojo / swr-react-native

React Native/React Navigation compatibility for Vercel's useSWR hook. 🐮

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Deduplication is broken in 1.1.2

coffeemug opened this issue · comments

I'm on swr-react-native 1.1.2 and swr 1.1.2. It looks like request deduplication is broken-- when I use useSWRNative and switch between react navigation screens, requests don't seem to get deduped. For example, a request to one url happens >30 times within a very brief period (just one render) when I switch focus or go to a new screen.

This doesn't happen when I call useSWR. Of course there is no revalidation on focus change, but even when new screens get mounted useSWR dedups the requests while useSWRNative doesn't.

For example, a request to one url happens >30 times within a very brief period (just one render) when I switch focus or go to a new screen.

Well that’s not good. I think I can guess for why this is happening. I assume that they are deduped relative to a single hook, but not across multiple hooks, since the deduping is done inside of the hook itself.

As a test case, if you just call the hook once on a screen, I would assume it dedupes based on interval, but not based on concurrent requests?

I think the reason is that we’re triggering revalidations by calling mutate(), and SWR may not dedupe on its own when you call this manually. I wonder if their latest version allows you to dedupe when you call mutate manually?

i’m looking at this: https://twitter.com/shuding_/status/1476608107623108611?s=21

I would want to know if there’s a way to do mutate(undefined, { dedupe: true })

Ah yep, looking at the code this appears to be the issue. It'd be great if their revalidate flag was more of an option, like always|never|dedup. Hopefully they add that 🤞

see my comment here: https://twitter.com/spakhm/status/1481799938342375426. I think their test covers a subtly different case where you pass a value to mutate.

Ah yep, looking at the code this appears to be the issue. It'd be great if their revalidate flag was more of an option, like always|never|dedup. Hopefully they add that 🤞

I can’t quickly think of an example of not deduping, but feel free to open a feature request in vercel/swr so that can be properly discussed! :)

see my comment here: https://twitter.com/spakhm/status/1481799938342375426. I think their test covers a subtly different case where you pass a value to mutate.

I’ll open an issue for this and take a look, thanks a lot!

thank you @shuding!

closing this in favor of vercel/swr#1786 on SWR's repo.