diff --git a/src/use-swr-infinite.ts b/src/use-swr-infinite.ts index 63d8b88bc9..2f48e15279 100644 --- a/src/use-swr-infinite.ts +++ b/src/use-swr-infinite.ts @@ -67,7 +67,8 @@ function useSWRInfinite( ) let { initialSize = 1, - revalidateAll = false, + // should revalidate pages when a refresh timer is invoked + revalidateAll = !!config.refreshInterval, persistSize = false, fetcher: defaultFetcher, ...extraConfig @@ -117,10 +118,11 @@ function useSWRInfinite( // keep the data inside a ref const dataRef = useRef() + const lastFetchedDataRef = useRef() // actual swr of all pages const swr = useSWR( - firstPageKey ? ['many', firstPageKey] : null, + firstPageKey ? ['many', firstPageKey, pageCountRef.current] : null, async () => { // get the revalidate context const { originalData, force } = cache.get(contextCacheKey) || {} @@ -146,15 +148,11 @@ function useSWRInfinite( // - `revalidateAll` is enabled // - `mutate()` called // - the cache is missing - // - it's the first page and it's not the first render // - cache has changed const shouldFetchPage = revalidateAll || force || typeof pageData === 'undefined' || - (typeof force === 'undefined' && - i === 0 && - typeof dataRef.current !== 'undefined') || (originalData && !config.compare(originalData[i], pageData)) if (shouldFetchPage) { @@ -168,6 +166,7 @@ function useSWRInfinite( data.push(pageData) previousPageData = pageData + lastFetchedDataRef.current = data } // once we executed the data fetching based on the context, clear the context @@ -176,7 +175,11 @@ function useSWRInfinite( // return the data return data }, - extraConfig + // do not pass initialData for updating to revalidate + { + ...extraConfig, + initialData: didMountRef.current ? undefined : extraConfig.initialData + } ) // update dataRef @@ -213,9 +216,8 @@ function useSWRInfinite( } cache.set(pageCountCacheKey, pageCountRef.current) rerender(v => !v) - return mutate(v => v) }, - [mutate, pageCountCacheKey] + [pageCountCacheKey] ) // Use getter functions to avoid unnecessary re-renders caused by triggering all the getters of the returned swr object @@ -226,7 +228,9 @@ function useSWRInfinite( enumerable: true }, data: { - get: () => swr.data, + get: () => + // return the last data when revalidating + swr.data !== undefined ? swr.data : lastFetchedDataRef.current, enumerable: true }, // revalidate will be deprecated in the 1.x release