From cc4bf7bb325f986f39c68fe9fd610dd5051df592 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Fri, 16 Jun 2023 00:14:22 +0200 Subject: [PATCH] Fix initial infinite swr request for pages > 1 (#367) 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. --- src/lib/RequestManager.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/RequestManager.ts b/src/lib/RequestManager.ts index ce72f381..8a2e34e2 100644 --- a/src/lib/RequestManager.ts +++ b/src/lib/RequestManager.ts @@ -66,8 +66,9 @@ export type AbortableSWRInfiniteResponse = 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) => {