Repository navigation
Prevent resolved promise to suspend due to missing status - #4271
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
c1b6f4f to
fdda24c
Compare
fdda24c to
ca63204
Compare
ca63204 to
3a7ea29
Compare
3a7ea29 to
183c68b
Compare
11b9461 to
9a7d5ae
Compare
0392aa8 to
80a3123
Compare
80a3123 to
8e0b002
Compare
| // Any fallback render means SWR suspended despite fulfilled fallback data. | ||
| const loadingFallback = screen.queryByText('loading') | ||
| expect(loadingFallback === null).toBe(true) | ||
| screen.getByText('fallback data') |
There was a problem hiding this comment.
does this account for the fallback flashing and then disappearing on a rerender? i.e. does this test actually fail before the change?
There was a problem hiding this comment.
8e0b002 to
5a260f5
Compare
| !isUndefined(req) && hasKeyButNoData ? boundMutate(req) : resolvedUndef | ||
| use(mutateReq) | ||
| if (!isUndefined(req) && hasKeyButNoData) { | ||
| use(boundMutate(req)) |
There was a problem hiding this comment.
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
|
|
||
| it('should not suspend when fallbackData is a fulfilled promise', async () => { | ||
| await withServer(async () => { | ||
| const useSWR = (await import('swr')).default |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
5a260f5 to
eee294c
Compare
eee294c to
2ee1c12
Compare
2ee1c12 to
6097d29
Compare
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
undefinedinstead, and because that promise did not havestatusattached, 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
undefinedpromise 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?
resolvedUndefpromise as fulfilled with anundefinedvalue souse(resolvedUndef)returns synchronously.fallbackDataand null-key Suspense paths; these tests fail before the fix and pass after it.