vercel / swr

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

Deduplication not working properly #3013

Open tastydev opened 2 months ago

tastydev commented 2 months ago

Bug report

Description / Observed Behavior

I am experiencing requests to the same endpoint/cache key within the dedupeInterval and i don't think this is the expected behaviour? In my code example posted below, you can see that 2 mounted react components are using the same custom hook to fetch a client ip. The custom hook is using swr to fetch the ip. The component registers an interval to simulate "same time" network requests to the same swr cache key.

Expected Behavior

I would expect to only see 1 request, if two components are using the same swr key while calling mutate within the dedupeInterval time span.

Repro Steps / Code Example

I made an example with my described behaviour here: CodeSandbox SWR Dedupe Example

  1. Open Dev Tools
  2. Open Network Tab
  3. See 2 requests to the same endpoint/key at the same time

Additional Context

SWR version: 2.2.5 but also seeing this in 1.3.0

pke commented 2 months ago

Related? https://github.com/vercel/swr/issues/1786#issuecomment-1013234154 I'm afraid this works as designed. How about not using mutate but instead just 2 components rendering the same key? Will it duplicate?

tastydev commented 2 months ago

It is related, but i am just not understanding why this might be the intended behavior. If code executes X mutates on the same key (while revalidate is true) for whatever reason there is no use case in my opinion why SWR should not deduplicate this. In my opinion mutates have to be queued or paused within the deduplication time span while one mutate is refreshing the cache with a request. After the request resolves, the other mutates are reading the cache. Obviously SWR is not doing this.... Is this a design flaw in SWR or are we just expecting to much from the library? What are your thoughts?