Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Only suspend when using the fallback #3045

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading