forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Float][Flight] Flight support for Float (facebook#26502)
Stacked on facebook#26557 Supporting Float methods such as ReactDOM.preload() are challenging for flight because it does not have an easy means to convey direct executions in other environments. Because the flight wire format is a JSON-like serialization that is expected to be rendered it currently only describes renderable elements. We need a way to convey a function invocation that gets run in the context of the client environment whether that is Fizz or Fiber. Fiber is somewhat straightforward because the HostDispatcher is always active and we can just have the FlightClient dispatch the serialized directive. Fizz is much more challenging becaue the dispatcher is always scoped but the specific request the dispatch belongs to is not readily available. Environments that support AsyncLocalStorage (or in the future AsyncContext) we will use this to be able to resolve directives in Fizz to the appropriate Request. For other environments directives will be elided. Right now this is pragmatic and non-breaking because all directives are opportunistic and non-critical. If this changes in the future we will need to reconsider how widespread support for async context tracking is. For Flight, if AsyncLocalStorage is available Float methods can be called before and after await points and be expected to work. If AsyncLocalStorage is not available float methods called in the sync phase of a component render will be captured but anything after an await point will be a noop. If a float call is dropped in this manner a DEV warning should help you realize your code may need to be modified. This PR also introduces a way for resources (Fizz) and hints (Flight) to flush even if there is not active task being worked on. This will help when Float methods are called in between async points within a function execution but the task is blocked on the entire function finishing. This PR also introduces deduping of Hints in Flight using the same resource keys used in Fizz. This will help shrink payload sizes when the same hint is attempted to emit over and over again
- Loading branch information
1 parent
f59d5aa
commit baff619
Showing
56 changed files
with
1,119 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
packages/react-dom-bindings/src/server/ReactDOMFlightServerHostDispatcher.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type { | ||
HostDispatcher, | ||
PrefetchDNSOptions, | ||
PreconnectOptions, | ||
PreloadOptions, | ||
PreinitOptions, | ||
} from 'react-dom/src/ReactDOMDispatcher'; | ||
|
||
import {enableFloat} from 'shared/ReactFeatureFlags'; | ||
|
||
import { | ||
emitHint, | ||
getHints, | ||
resolveRequest, | ||
} from 'react-server/src/ReactFlightServer'; | ||
|
||
export const ReactDOMFlightServerDispatcher: HostDispatcher = { | ||
prefetchDNS, | ||
preconnect, | ||
preload, | ||
preinit, | ||
}; | ||
|
||
function prefetchDNS(href: string, options?: ?PrefetchDNSOptions) { | ||
if (enableFloat) { | ||
if (typeof href === 'string') { | ||
const request = resolveRequest(); | ||
if (request) { | ||
const hints = getHints(request); | ||
const key = 'D' + href; | ||
if (hints.has(key)) { | ||
// duplicate hint | ||
return; | ||
} | ||
hints.add(key); | ||
if (options) { | ||
emitHint(request, 'D', [href, options]); | ||
} else { | ||
emitHint(request, 'D', href); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function preconnect(href: string, options: ?PreconnectOptions) { | ||
if (enableFloat) { | ||
if (typeof href === 'string') { | ||
const request = resolveRequest(); | ||
if (request) { | ||
const hints = getHints(request); | ||
const crossOrigin = | ||
options == null || typeof options.crossOrigin !== 'string' | ||
? null | ||
: options.crossOrigin === 'use-credentials' | ||
? 'use-credentials' | ||
: ''; | ||
|
||
const key = `C${crossOrigin === null ? 'null' : crossOrigin}|${href}`; | ||
if (hints.has(key)) { | ||
// duplicate hint | ||
return; | ||
} | ||
hints.add(key); | ||
if (options) { | ||
emitHint(request, 'C', [href, options]); | ||
} else { | ||
emitHint(request, 'C', href); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function preload(href: string, options: PreloadOptions) { | ||
if (enableFloat) { | ||
if (typeof href === 'string') { | ||
const request = resolveRequest(); | ||
if (request) { | ||
const hints = getHints(request); | ||
const key = 'L' + href; | ||
if (hints.has(key)) { | ||
// duplicate hint | ||
return; | ||
} | ||
hints.add(key); | ||
emitHint(request, 'L', [href, options]); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function preinit(href: string, options: PreinitOptions) { | ||
if (enableFloat) { | ||
if (typeof href === 'string') { | ||
const request = resolveRequest(); | ||
if (request) { | ||
const hints = getHints(request); | ||
const key = 'I' + href; | ||
if (hints.has(key)) { | ||
// duplicate hint | ||
return; | ||
} | ||
hints.add(key); | ||
emitHint(request, 'I', [href, options]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.