From 9f36b40a6dc334d42f75ea9d44208927e729c526 Mon Sep 17 00:00:00 2001 From: Simen Owesen-Lein Date: Thu, 10 Feb 2022 09:58:13 +0100 Subject: [PATCH] bugfix: make suspense and revalidateIfStale work together --- src/use-swr.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/use-swr.ts b/src/use-swr.ts index e8aa6f08e..12a3314e4 100644 --- a/src/use-swr.ts +++ b/src/use-swr.ts @@ -97,13 +97,14 @@ export const useSWRHandler = ( // If it's paused, we skip revalidation. if (getConfig().isPaused()) return false - return suspense - ? // Under suspense mode, it will always fetch on render if there is no - // stale data so no need to revalidate immediately on mount again. - !isUndefined(data) - : // If there is no stale data, we need to revalidate on mount; - // If `revalidateIfStale` is set to true, we will always revalidate. - isUndefined(data) || config.revalidateIfStale + // Under suspense mode, it will always fetch on render if there is no + // stale data so no need to revalidate immediately on mount again. + // If data exists, only revalidate if `revalidateIfStale` is true. + if (suspense) return isUndefined(data) ? false : config.revalidateIfStale + + // If there is no stale data, we need to revalidate on mount; + // If `revalidateIfStale` is set to true, we will always revalidate. + return isUndefined(data) || config.revalidateIfStale } // Resolve the current validating state.