-
Notifications
You must be signed in to change notification settings - Fork 47k
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
Allow uncached IO to stablize #25561
Conversation
Comparing: 6883d79...401230e Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: Expand to show |
b54bb03
to
5ce16df
Compare
5ce16df
to
4cc9c19
Compare
// If this fiber just suspended, it's possible the data is already | ||
// cached. Yield to the main thread to give it a chance to ping. If | ||
// it does, we can retry immediately without unwinding the stack. | ||
workInProgressSuspendedReason = SuspendedAndReadyToUnwind; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right here is where I'll eventually add the microtask thingy for discrete updates
Initial draft. I need to test this more. If a promise is passed to `use`, but the I/O isn't cached, we should still be able to unwrap it. This already worked in Server Components, and during SSR. For Fiber (in the browser), before this fix the state would get lost between attempts unless the promise resolved immediately in a microtask, which requires IO to be cached. This was due to an implementation quirk of Fiber where the state is reset as soon as the stack unwinds. The workaround is to suspend the entire Fiber work loop until the promise resolves. The Server Components and SSR runtimes don't require a workaround: they can maintain multiple parallel child tasks and reuse the state indefinitely across attempts. That's ideally how Fiber should work, too, but it will require larger refactor. The downside of our approach in Fiber is that it won't "warm up" the siblings while you're suspended, but to avoid waterfalls you're supposed to hoist data fetches higher in the tree regardless. But we have other ideas for how we can add this back in the future. (Though again, this doesn't affect Server Components, which already have the ideal behavior.)
4cc9c19
to
401230e
Compare
if (suspenseHandler !== null && suspenseHandler.tag === SuspenseComponent) { | ||
const currentSuspenseHandler = suspenseHandler.alternate; | ||
const nextProps: SuspenseProps = suspenseHandler.memoizedProps; | ||
if (isBadSuspenseFallback(currentSuspenseHandler, nextProps)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we know this based on the Suspense context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The context stores the nearest "active" handler but it doesn't current track whether it's an avoided fallback condition, though it could. But that's effectively what this check does. It also checks if it's inside an offscreen tree:
react/packages/react-reconciler/src/ReactFiberSuspenseContext.new.js
Lines 61 to 86 in 6883d79
export function isBadSuspenseFallback( | |
current: Fiber | null, | |
nextProps: SuspenseProps, | |
): boolean { | |
// Check if this is a "bad" fallback state or a good one. A bad fallback state | |
// is one that we only show as a last resort; if this is a transition, we'll | |
// block it from displaying, and wait for more data to arrive. | |
if (current !== null) { | |
const prevState: SuspenseState = current.memoizedState; | |
const isShowingFallback = prevState !== null; | |
if (!isShowingFallback && !isCurrentTreeHidden()) { | |
// It's bad to switch to a fallback if content is already visible | |
return true; | |
} | |
} | |
if ( | |
enableSuspenseAvoidThisFallback && | |
nextProps.unstable_avoidThisFallback === true | |
) { | |
// Experimental: Some fallbacks are always bad | |
return true; | |
} | |
return false; | |
} |
* 'main' of ssh://github.com/GrinZero/react: (163 commits) Do not unmount layout effects if ancestor Offscreen is hidden (facebook#25628) Remove check in renderDidSuspendDelayIfPossible (facebook#25630) [ServerRenderer] Move fizz external runtime implementation to react-dom-bindings (facebook#25617) Unwrap sync resolved thenables without suspending (facebook#25615) refactor isHostResourceType to not receive the context from reconciler and not leak types (facebook#25610) Make host context use null as empty and only error in dev (facebook#25609) [Float] handle resource Resource creation inside svg context (facebook#25599) Allow uncached IO to stablize (facebook#25561) [ServerRenderer] Setup for adding data attributes streaming format (facebook#25567) Do not unmount layout effects on initial Offscreen mount (facebook#25592) Fix type check for null (facebook#25595) Clean up vestige of useOpaqueIdentifier (facebook#25587) Extract logic for detecting bad fallback to helper Split suspended work loop logic into separate functions In work loop, add enum of reasons for suspending Strict Mode: Reuse memoized result from first pass (facebook#25583) Detect and warn if use(promise) is wrapped with try/catch block (facebook#25543) Remove old react-fetch, react-fs and react-pg libraries (facebook#25577) Try assigning fetch to globalThis if global assignment fails (facebook#25571) [Float] handle noscript context for Resources (facebook#25559) ... # Conflicts: # scripts/rollup/build.js
Summary: This sync includes the following changes: - **[4bd245e9e](facebook/react@4bd245e9e )**: Do not unmount layout effects if ancestor Offscreen is hidden ([#25628](facebook/react#25628)) //<Samuel Susla>// - **[df61e708c](facebook/react@df61e708c )**: Remove check in renderDidSuspendDelayIfPossible ([#25630](facebook/react#25630)) //<Andrew Clark>// - **[1a08f1478](facebook/react@1a08f1478 )**: [ServerRenderer] Move fizz external runtime implementation to react-dom-bindings ([#25617](facebook/react#25617)) //<mofeiZ>// - **[1a902623a](facebook/react@1a902623a )**: Unwrap sync resolved thenables without suspending ([#25615](facebook/react#25615)) //<Andrew Clark>// - **[4ea063b56](facebook/react@4ea063b56 )**: refactor isHostResourceType to not receive the context from reconciler and not leak types ([#25610](facebook/react#25610)) //<Josh Story>// - **[8e69bc45a](facebook/react@8e69bc45a )**: Make host context use null as empty and only error in dev ([#25609](facebook/react#25609)) //<Sebastian Markbåge>// - **[5f7ef8c4c](facebook/react@5f7ef8c4c )**: [Float] handle resource Resource creation inside svg context ([#25599](facebook/react#25599)) //<Josh Story>// - **[36426e6cb](facebook/react@36426e6cb )**: Allow uncached IO to stablize ([#25561](facebook/react#25561)) //<Andrew Clark>// - **[6883d7944](facebook/react@6883d7944 )**: [ServerRenderer] Setup for adding data attributes streaming format ([#25567](facebook/react#25567)) //<mofeiZ>// Changelog: [General][Changed] - React Native sync for revisions ab075a2...4bd245e jest_e2e[run_all_tests] Reviewed By: GijsWeterings Differential Revision: D41028209 fbshipit-source-id: a67fdcd441ddd50784f7c1ce402eaecdb5e3126d
Initial draft. I need to test this more. If a promise is passed to `use`, but the I/O isn't cached, we should still be able to unwrap it. This already worked in Server Components, and during SSR. For Fiber (in the browser), before this fix the state would get lost between attempts unless the promise resolved immediately in a microtask, which requires IO to be cached. This was due to an implementation quirk of Fiber where the state is reset as soon as the stack unwinds. The workaround is to suspend the entire Fiber work loop until the promise resolves. The Server Components and SSR runtimes don't require a workaround: they can maintain multiple parallel child tasks and reuse the state indefinitely across attempts. That's ideally how Fiber should work, too, but it will require larger refactor. The downside of our approach in Fiber is that it won't "warm up" the siblings while you're suspended, but to avoid waterfalls you're supposed to hoist data fetches higher in the tree regardless. But we have other ideas for how we can add this back in the future. (Though again, this doesn't affect Server Components, which already have the ideal behavior.)
Summary: This sync includes the following changes: - **[4bd245e9e](facebook/react@4bd245e9e )**: Do not unmount layout effects if ancestor Offscreen is hidden ([facebook#25628](facebook/react#25628)) //<Samuel Susla>// - **[df61e708c](facebook/react@df61e708c )**: Remove check in renderDidSuspendDelayIfPossible ([facebook#25630](facebook/react#25630)) //<Andrew Clark>// - **[1a08f1478](facebook/react@1a08f1478 )**: [ServerRenderer] Move fizz external runtime implementation to react-dom-bindings ([facebook#25617](facebook/react#25617)) //<mofeiZ>// - **[1a902623a](facebook/react@1a902623a )**: Unwrap sync resolved thenables without suspending ([facebook#25615](facebook/react#25615)) //<Andrew Clark>// - **[4ea063b56](facebook/react@4ea063b56 )**: refactor isHostResourceType to not receive the context from reconciler and not leak types ([facebook#25610](facebook/react#25610)) //<Josh Story>// - **[8e69bc45a](facebook/react@8e69bc45a )**: Make host context use null as empty and only error in dev ([facebook#25609](facebook/react#25609)) //<Sebastian Markbåge>// - **[5f7ef8c4c](facebook/react@5f7ef8c4c )**: [Float] handle resource Resource creation inside svg context ([facebook#25599](facebook/react#25599)) //<Josh Story>// - **[36426e6cb](facebook/react@36426e6cb )**: Allow uncached IO to stablize ([facebook#25561](facebook/react#25561)) //<Andrew Clark>// - **[6883d7944](facebook/react@6883d7944 )**: [ServerRenderer] Setup for adding data attributes streaming format ([facebook#25567](facebook/react#25567)) //<mofeiZ>// Changelog: [General][Changed] - React Native sync for revisions ab075a2...4bd245e jest_e2e[run_all_tests] Reviewed By: GijsWeterings Differential Revision: D41028209 fbshipit-source-id: a67fdcd441ddd50784f7c1ce402eaecdb5e3126d
Initial draft. I need to test this more.
If a promise is passed to
use
, but the I/O isn't cached, we should still be able to unwrap it.This already worked in Server Components, and during SSR.
For Fiber (in the browser), before this fix the state would get lost between attempts unless the promise resolved immediately in a microtask, which requires IO to be cached. This was due to an implementation quirk of Fiber where the state is reset as soon as the stack unwinds. The workaround is to suspend the entire Fiber work loop until the promise resolves.
The Server Components and SSR runtimes don't require a workaround: they can maintain multiple parallel child tasks and reuse the state indefinitely across attempts. That's ideally how Fiber should work, too, but it will require larger refactor.
The downside of our approach in Fiber is that it won't "warm up" the siblings while you're suspended, but to avoid waterfalls you're supposed to hoist data fetches higher in the tree regardless. But we have other ideas for how we can add this back in the future. (Though again, this doesn't affect Server Components, which already have the ideal behavior.)