Skip to content

Prevent resolved promise to suspend due to missing status - #4271

Merged
huozhi merged 3 commits into
mainfrom
jiwon/resolved-undef-regression-test
Jun 19, 2026
Merged

huozhi merged 3 commits into
mainfrom
jiwon/resolved-undef-regression-test

Conversation

@devjiwonchoi

@devjiwonchoi devjiwonchoi commented Jun 17, 2026 •

Copy link
Copy Markdown
Member

Why?

Current behavior: https://github.com/vercel/swr/actions/runs/27748306911/job/82092031539?pr=4271#step:4:714

In SWR Suspense mode, when the promise is already fulfilled, React does not have to suspend. However, there are paths where SWR consumes an internal promise that resolves to undefined instead, and because that promise did not have status attached, React could not know it's status on the initial consume, so it suspended.

This caused the hook to unnecessarily suspend in conditions where it did not really have to on the client, such as during a sync update or hydration, which could show a Suspense fallback even after the SSR'd content has been rendered.

By marking the internal undefined promise as fulfilled, SWR ensures consuming that does not suspend and only suspends when the actual data promise is not fulfilled.

React thenable x-ref: https://github.com/react/react/blob/560db51408f4c186016ccdd8062af75a77c22104/packages/react-reconciler/src/ReactFiberThenable.js#L187-L302

How?

  • Mark SWR's internal resolvedUndef promise as fulfilled with an undefined value so use(resolvedUndef) returns synchronously.
  • Add regression coverage for fulfilled promise fallbackData and null-key Suspense paths; these tests fail before the fix and pass after it.

@vercel

vercel Bot commented Jun 17, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
swr Error Error Jun 19, 2026 3:49pm

@devjiwonchoi
devjiwonchoi force-pushed the jiwon/resolved-undef-regression-test branch from c1b6f4f to fdda24c Compare June 17, 2026 20:03
@devjiwonchoi
devjiwonchoi force-pushed the jiwon/resolved-undef-regression-test branch from fdda24c to ca63204 Compare June 18, 2026 04:26
@devjiwonchoi
devjiwonchoi force-pushed the jiwon/resolved-undef-regression-test branch from ca63204 to 3a7ea29 Compare June 18, 2026 04:30
@devjiwonchoi
devjiwonchoi force-pushed the jiwon/resolved-undef-regression-test branch from 3a7ea29 to 183c68b Compare June 18, 2026 04:38
@devjiwonchoi
devjiwonchoi force-pushed the jiwon/resolved-undef-regression-test branch from 11b9461 to 9a7d5ae Compare June 18, 2026 08:55
@devjiwonchoi
devjiwonchoi force-pushed the jiwon/resolved-undef-regression-test branch from 0392aa8 to 80a3123 Compare June 18, 2026 09:19
@devjiwonchoi
devjiwonchoi force-pushed the jiwon/resolved-undef-regression-test branch from 80a3123 to 8e0b002 Compare June 18, 2026 10:32
Comment on lines +122 to +125
// Any fallback render means SWR suspended despite fulfilled fallback data.
const loadingFallback = screen.queryByText('loading')
expect(loadingFallback === null).toBe(true)
screen.getByText('fallback data')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this account for the fallback flashing and then disappearing on a rerender? i.e. does this test actually fail before the change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devjiwonchoi
devjiwonchoi force-pushed the jiwon/resolved-undef-regression-test branch from 8e0b002 to 5a260f5 Compare June 18, 2026 17:07
@devjiwonchoi
devjiwonchoi marked this pull request as ready for review June 19, 2026 12:25
@devjiwonchoi devjiwonchoi changed the title Add resolved undefined suspense regression test Prevent resolved promise to suspend due to missing status Jun 19, 2026
Comment thread src/index/use-swr.ts Outdated
!isUndefined(req) && hasKeyButNoData ? boundMutate(req) : resolvedUndef
use(mutateReq)
if (!isUndefined(req) && hasKeyButNoData) {
use(boundMutate(req))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have to make use() under condition? Seb raised an issue related to it before so we changed. I'm not sure if this is still an issue with React

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

x-ref: react/react#34030


it('should not suspend when fallbackData is a fulfilled promise', async () => {
await withServer(async () => {
const useSWR = (await import('swr')).default

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that possible to make a e2e test instead of mocking a lot with this e2e test, in that way we can avoid mocking react internal states

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried write e2e but wasn't able to capture the fallback state because React just commits the content before even show fallback, likely as it handles in a micro task and the fixture is lightweight that it just commits the rerender before the fallback

@huozhi
huozhi merged commit a89bbe7 into main Jun 19, 2026
8 checks passed
@huozhi
huozhi deleted the jiwon/resolved-undef-regression-test branch June 19, 2026 17:23

This branch had an error being deployed

1 failed deployment
Preview — 6097d293 Deployed Jun 19, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants