Skip to content

Commit

Permalink
apply code review
Browse files Browse the repository at this point in the history
  • Loading branch information
promer94 committed Dec 27, 2022
1 parent c9db21a commit 1cd75d3
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions core/use-swr.ts
Expand Up @@ -183,6 +183,10 @@ export const useSWRHandler = <Data = any, Error = any>(
)

const isInitialMount = !initialMountedRef.current

const hasRevalidator =
EVENT_REVALIDATORS[key] && EVENT_REVALIDATORS[key].length > 0

const cachedData = cached.data

const data = isUndefined(cachedData) ? fallback : cachedData
Expand All @@ -201,6 +205,9 @@ export const useSWRHandler = <Data = any, Error = any>(
// - Not suspense mode and there is no fallback data and `revalidateIfStale` is enabled.
// - `revalidateIfStale` is enabled but `data` is not defined.
const shouldDoInitialRevalidation = (() => {
// if a key already has revalidators and also has error, we should not trigger revalidation
if (hasRevalidator && !isUndefined(error)) return false

// If `revalidateOnMount` is set, we take the value directly.
if (isInitialMount && !isUndefined(revalidateOnMount))
return revalidateOnMount
Expand Down Expand Up @@ -511,8 +518,6 @@ export const useSWRHandler = <Data = any, Error = any>(
return
}

const isKeyHasActiveInstance =
EVENT_REVALIDATORS[key] && EVENT_REVALIDATORS[key].length > 0
const unsubEvents = subscribeCallback(key, EVENT_REVALIDATORS, onRevalidate)

// Mark the component as mounted and update corresponding refs.
Expand All @@ -523,9 +528,6 @@ export const useSWRHandler = <Data = any, Error = any>(
// Keep the original key in the cache.
setCache({ _k: fnArg })

// if a key is already active and has error, we should not trigger revalidation
if (isKeyHasActiveInstance && !isUndefined(error)) return

// Trigger a revalidation.
if (shouldDoInitialRevalidation) {
if (isUndefined(data) || IS_SERVER) {
Expand Down

0 comments on commit 1cd75d3

Please sign in to comment.