vercel / swr

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

Infinite does not revalidate all pages when the zeroth one changes #2969

Open alvvaro opened 4 months ago

alvvaro commented 4 months ago

Bug report

Description / Observed Behavior

As I've come to understand, SWR Infinite by default always fetches the zeroth page alongside the current one. The data is then compared to the cache, so that if anything on it has changed, it fetches again all pages at once. https://github.com/vercel/swr/issues/1401#issuecomment-907764278

I cannot seem to trigger this behavior. When the zeroth page changes, all middle pages remain the same, which produces skips and duplicate keys in the list.

Expected Behavior

With the default configuration, SWR should revalidate all pages if the zeroth page does not correspond to its cache.

Repro Steps / Code Example

I've modified the SWR Infinite example of the docs to add a +1 button that appends a new item to the beginning of the list. Click +1 any number of times and then click load more and see how it starts to become more and more inconsistent. The data in the codesandbox is purely local, but I'm able to trigger this with a live API. https://codesandbox.io/p/sandbox/swr-infinite-forked-qltkgj

ScreenRecording2024-05-18at12 45 48-ezgif com-video-to-gif-converter

Additional Context

The codesandbox I linked uses v2, but I noticed this on my app which has v2.2.5.

koba04 commented 3 months ago

I cannot seem to trigger this behavior. When the zeroth page changes, all middle pages remain the same, which produces skips and duplicate keys in the list.

I think this behavior is for cursor-based API, but your example is a page-based API.

alvvaro commented 3 months ago

You're right, I misread the comment from shuding. He was explicitly talking about cursor-based APIs.

However I'm now more confused, since a cursor sidesteps the content shift problem entirely. The revalidation waterfall he talks about is not necessary in that case? Can this waterfall even happen, like is it a feature that exists in code?

Sorry, I'll be happy to close this issue and open a discussion about this, because I was clearly mistaken.