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

Fix the revalidation condition of the first page #895

Merged
merged 5 commits into from
Jan 16, 2021

Conversation

shuding
Copy link
Member

@shuding shuding commented Jan 12, 2021

This PR fixes a useSWRInfinite bug introduced in #799 by me (thanks @pacocoursey for reporting). To reproduce, just simply use refreshInterval with useSWRInfinite, and the first page will never be revalidated:

Reproduction: https://codesandbox.io/s/swr-infinite-forked-mi7rf?file=/src/App.js

Postmortem and Fix

The cause of the bug is this LOC:

(typeof force === 'undefined' && i === 0 && originalData) ||

...to determine if we should fetch this page or not, which can break down into:

  • typeof force === 'undefined: It's not a force revalidation, like mutate().
  • i === 0: It's the first page, we don't want to revalidate every page upon focus.
  • originalData: It's not the first render. This is the mistake.

What we want to achieve for useSWRInfinite is this experience: If we have the cache and the screen is empty (first render), we display the cache ASAP. So we added the third condition above. But we should check dataRef instead of originalData (originalData is specifically for mutations).

Also added another test.

@shuding shuding marked this pull request as ready for review January 12, 2021 15:35
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 12, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit ed9e2d1:

Sandbox Source
SWR-Basic Configuration
SWR-States Configuration
SWR-Infinite Configuration
SWR-Infinite (forked) PR

Copy link
Member

@huozhi huozhi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm from test case perspective

@shuding shuding merged commit f6a6c6a into master Jan 16, 2021
@shuding shuding deleted the fix-use-swr-infinite-revalidation branch January 16, 2021 13:02
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

Successfully merging this pull request may close these issues.

2 participants