Skip to content

Commit

Permalink
Fix initial infinite swr request for pages > 1 (#367)
Browse files Browse the repository at this point in the history
In case more than 1 page was requested for the initial request, there was a problem in case the first page already had no results.
In that case the request for the second page always returned with the "isLoadMore" and "isLoading" flag set to true.
Thus, incorrectly indicating that the request is still active.
The "actual" request was already finished, but internally the "isLoadMore" flag was set incorrectly due to not considering if an actual request is active.
  • Loading branch information
schroda authored Jun 15, 2023
1 parent dcd5302 commit cc4bf7b
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ export type AbortableSWRInfiniteResponse<Data = any, Error = any> = SWRInfiniteR

const isLoadingMore = (swrResult: SWRInfiniteResponse): boolean => {
const isNextPageMissing = !!swrResult.data && typeof swrResult.data[swrResult.size - 1] === 'undefined';
const isRequestActive = swrResult.isValidating;
// SWR "isLoading" state is only updated for the first load, for every subsequent load it's "false"
return !swrResult.isLoading && swrResult.size > 0 && isNextPageMissing;
return !swrResult.isLoading && swrResult.size > 0 && isNextPageMissing && isRequestActive;
};

const disableSwrInfiniteCache: Middleware = (useSWRNext) => (key, fetcher, config) => {
Expand Down

0 comments on commit cc4bf7b

Please sign in to comment.