Skip to content
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

useSWRInfinite with revalidateAll: true revalidates all pages when loading next page #2982

Open
samnoyes opened this issue Jun 13, 2024 · 0 comments

Comments

@samnoyes
Copy link

This is a reopen of #590. I have added a repro in CodeSandbox. cc @shuding

Bug report

Description / Observed Behavior

I'm using useSWRInfinite with revalidateAll: true to revalidate all cached pages when component mounts or on focus. But it causes revalidation of all pages when loading next page.

const { size, setSize } = useSWRInfinite(
  (index) =`https://example.com/issues?page=${index   1}`,
  fetcher,
  { revalidateAll: true }
);

return (
  <button onClick={() =setSize(size   1)}>Load More</button
)

Clicking "Load more" button two times will cause 5 requests: https://example.com/issues?page=1 https://example.com/issues?page=2 https://example.com/issues?page=1 https://example.com/issues?page=2 https://example.com/issues?page=3

So loading N pages will cause N(N 1)/2 requests.

Expected Behavior

I expect useSWRInfinite with revalidateAll: true not to revalidate all pages when loading next page but to revalidate all pages on focus, reconnect, mount etc (similar to useSWR hook).

Right now I can do something like this to achieve desirable behavior but it feels hacky:

const { size, setSize, mutate } = useSWRInfinite(
  (index) =`https://example.com/issues?page=${index   1}`,
  fetcher
);

const latestMutate = useRef(mutate)
latestMutate.current = mutate

useEffect(() ={
  latestMutate.current()
}, [])

Additional Context

SWR v2.1.5

CodeSandbox Repro

Here is a repro of the issue. click load more with the network tab open and you will see N requests on each click

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant