Skip to content

Commit

Permalink
fix: Only suspend when using the fallback (#3045)
Browse files Browse the repository at this point in the history
* only suspend when using the fallback

* lint: let -> const

---------

Co-authored-by: Jiachi Liu <[email protected]>
  • Loading branch information
shuding and huozhi authored Dec 5, 2024
1 parent bd839a4 commit b30df29
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/core/use-swr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,11 @@ export const useSWRHandler = <Data = any, Error = any>(

// Resolve the fallback data from either the inline option, or the global provider.
// If it's a promise, we simply let React suspend and resolve it for us.
let fallback = isUndefined(fallbackData)
const fallback = isUndefined(fallbackData)
? isUndefined(config.fallback)
? UNDEFINED
: config.fallback[key]
: fallbackData
if (fallback && isPromiseLike(fallback)) {
fallback = use(fallback)
}

const isEqual = (prev: State<Data, any>, current: State<Data, any>) => {
for (const _ in stateDependencies) {
Expand Down Expand Up @@ -269,7 +266,11 @@ export const useSWRHandler = <Data = any, Error = any>(

const cachedData = cached.data

const data = isUndefined(cachedData) ? fallback : cachedData
const data = isUndefined(cachedData)
? (fallback && isPromiseLike(fallback))
? use(fallback)
: fallback
: cachedData
const error = cached.error

// Use a ref to store previously returned data. Use the initial data as its initial value.
Expand Down

0 comments on commit b30df29

Please sign in to comment.