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

revalidateOnMount With Fallback triggers isLoading instead of isValidating #3046

Open
anaclumos opened this issue Dec 5, 2024 · 2 comments

Comments

@anaclumos
Copy link

anaclumos commented Dec 5, 2024

Bug report

Description / Observed Behavior

What kind of issues did you encounter with SWR?

I have Next App Router Server Fetch to cache the initial response and then use SWR to have live-updates. I pass the initial server fetch as fallback data to simplify the code.

When a user interacts and changes the entire behavior, there are places I need to set isLoading to show that the user's interaction triggered a loading (imagine "jump to page 30 of the table"). So I have like isLoading ? <Skeleton /> : <Content />.

However, even when SWR has fallback data, it first triggers isLoading.

Expected Behavior

How did you expect SWR to behave here?

It should trigger isValidating instead of isLoading, since the initial data has been provided.


Or to be more specific about my request, I'd like to know how Prerendering (or Server-side caching) with SWR should work on App Router.

I want first to show the server fetch-cached data, then silently revalidate and update on the client side if anything is different. Meanwhile, passing as a fallback like TanStack Query triggers isLoading to be true, showing the skeleton.

You might ask, "Why set isLoading at all when the server fallback data would always be present?" that's because when a user types something on the search box with instant search, say /?q=somesearchquery, this should only be done with some search param useState with useSWR. However, in this scenario, since there is no isLoading, the user would see some stale data (or wrong data) then see the result. Thus, I want to use isLoading in this case.

@anaclumos
Copy link
Author

anaclumos commented Dec 5, 2024

Or to be more specific about my request, I'd like to know how Prerendering (or Server-side caching) with SWR should work on App Router.

I want first to show the server fetch-cached data, then silently revalidate and update on the client side if anything is different. Meanwhile, passing as a fallback like TanStack Query triggers isLoading to be true, showing the skeleton.

You might ask, "Why set isLoading at all when the server fallback data would always be present?" that's because when a user types something on the search box with instant search, say /?q=somesearchquery, this should only be done with some search param useState with useSWR. However, in this scenario, since there is no isLoading, the user would see some stale data (or wrong data) then see the result. Thus, I want to use isLoading in this case.

@anaclumos
Copy link
Author

Setting const [initialRevalidationSuccess, setInitialRevalidationSuccess] = useState(false);, set the value to true at the fetcher, and renaming the return value to isLoading: isLoading && initialRevalidationSuccess temporarily fixes the issue.

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