Skip to content

Commit 47fd6ca

Browse files
committed
Allow Float methods to be called anytime in Fiber
Previously we restricted Float methods to only being callable while rendering. This allowed us to make associations between calls and their position in the DOM tree, for instance hoisting preinitialized styles into a ShadowRoot or an iframe Document. When considering how we are going to support Flight support in Float however it became clear that this restriction would lead to compromises on the implementation because the Flight client does not execute within the context of a client render. We want to be able to disaptch Float directives coming from Flight as soon as possible and this requires being able to call them outside of render. this patch modifies Float so that its methods are callable anywhere. The main consequence of this change is Float will always use the Document the renderer script is running within as the HoistableRoot. This means if you preinit as style inside a component render targeting a ShadowRoot the style will load in the ownerDocument not the ShadowRoot. Practially speaking it means that preinit is not useful inside ShadowRoots and iframes. This tradeoff was deemed acceptable because these methods are optimistic, not critical. Additionally, the other methods, preconntect, prefetchDNS, and preload, are not impacted because they already operated at the level of the ownerDocument and really only interface with the Network cache layer. When Float was first implemented the HostDispatcher was set and unset during each render. Now that we support dispatching globally the host extensions that did this Dispatcher shuffling is now a noop. Since these methods exist in all HostConfigs and they noop everywhere now we shoudl just remove them.
1 parent 1216d4c commit 47fd6ca

File tree

11 files changed

+80
-164
lines changed

11 files changed

+80
-164
lines changed

packages/react-art/src/ReactARTHostConfig.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,3 @@ export function suspendInstance(type, props) {}
475475
export function waitForCommitToBeReady() {
476476
return null;
477477
}
478-
// eslint-disable-next-line no-undef
479-
export function prepareRendererToRender(container: Container): void {
480-
// noop
481-
}
482-
483-
export function resetRendererAfterRender(): void {
484-
// noop
485-
}

packages/react-dom-bindings/src/client/ReactDOMHostConfig.js

Lines changed: 31 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import {ConcurrentMode, NoMode} from 'react-reconciler/src/ReactTypeOfMode';
2525

2626
import hasOwnProperty from 'shared/hasOwnProperty';
2727
import {checkAttributeStringCoercion} from 'shared/CheckStringCoercion';
28-
import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals.js';
29-
const {Dispatcher} = ReactDOMSharedInternals;
3028

3129
import {
3230
precacheFiberNode,
@@ -1933,31 +1931,6 @@ export function prepareToCommitHoistables() {
19331931
tagCaches = null;
19341932
}
19351933

1936-
// It is valid to preload even when we aren't actively rendering. For cases where Float functions are
1937-
// called when there is no rendering we track the last used document. It is not safe to insert
1938-
// arbitrary resources into the lastCurrentDocument b/c it may not actually be the document
1939-
// that the resource is meant to apply too (for example stylesheets or scripts). This is only
1940-
// appropriate for resources that don't really have a strict tie to the document itself for example
1941-
// preloads
1942-
let lastCurrentDocument: ?Document = null;
1943-
let previousDispatcher = null;
1944-
export function prepareRendererToRender(rootContainer: Container) {
1945-
if (enableFloat) {
1946-
const rootNode = getHoistableRoot(rootContainer);
1947-
lastCurrentDocument = getDocumentFromRoot(rootNode);
1948-
1949-
previousDispatcher = Dispatcher.current;
1950-
Dispatcher.current = ReactDOMClientDispatcher;
1951-
}
1952-
}
1953-
1954-
export function resetRendererAfterRender() {
1955-
if (enableFloat) {
1956-
Dispatcher.current = previousDispatcher;
1957-
previousDispatcher = null;
1958-
}
1959-
}
1960-
19611934
// global collections of Resources
19621935
const preloadPropsMap: Map<string, PreloadProps> = new Map();
19631936
const preconnectsSet: Set<string> = new Set();
@@ -1979,25 +1952,6 @@ function getCurrentResourceRoot(): null | HoistableRoot {
19791952
return currentContainer ? getHoistableRoot(currentContainer) : null;
19801953
}
19811954

1982-
// Preloads are somewhat special. Even if we don't have the Document
1983-
// used by the root that is rendering a component trying to insert a preload
1984-
// we can still seed the file cache by doing the preload on any document we have
1985-
// access to. We prefer the currentDocument if it exists, we also prefer the
1986-
// lastCurrentDocument if that exists. As a fallback we will use the window.document
1987-
// if available.
1988-
function getDocumentForPreloads(): ?Document {
1989-
const root = getCurrentResourceRoot();
1990-
if (root) {
1991-
return root.ownerDocument || root;
1992-
} else {
1993-
try {
1994-
return lastCurrentDocument || window.document;
1995-
} catch (error) {
1996-
return null;
1997-
}
1998-
}
1999-
}
2000-
20011955
function getDocumentFromRoot(root: HoistableRoot): Document {
20021956
return root.ownerDocument || root;
20031957
}
@@ -2012,13 +1966,23 @@ export const ReactDOMClientDispatcher = {
20121966
preinit,
20131967
};
20141968

1969+
// We expect this to get inlined. It is a function mostly to communicate the special nature of
1970+
// how we resolve the HoistableRoot for ReactDOM.pre*() methods. Because we support calling
1971+
// these methods outside of render there is no way to know which Document or ShadowRoot is 'scoped'
1972+
// and so we have to fall back to something universal. Currently we just refer to the global document.
1973+
// This is notable because nowhere else in ReactDOM do we actually reference the global document or window
1974+
// because we may be rendering inside an iframe.
1975+
function getDocumentForImperativeFloatMethods(): Document {
1976+
return document;
1977+
}
1978+
20151979
function preconnectAs(
20161980
rel: 'preconnect' | 'dns-prefetch',
20171981
crossOrigin: null | '' | 'use-credentials',
20181982
href: string,
20191983
) {
2020-
const ownerDocument = getDocumentForPreloads();
2021-
if (typeof href === 'string' && href && ownerDocument) {
1984+
const ownerDocument = getDocumentForImperativeFloatMethods();
1985+
if (typeof href === 'string' && href) {
20221986
const limitedEscapedHref =
20231987
escapeSelectorAttributeValueInsideDoubleQuotes(href);
20241988
let key = `link[rel="${rel}"][href="${limitedEscapedHref}"]`;
@@ -2040,6 +2004,9 @@ function preconnectAs(
20402004
}
20412005
20422006
function prefetchDNS(href: string, options?: mixed) {
2007+
if (!enableFloat) {
2008+
return;
2009+
}
20432010
if (__DEV__) {
20442011
if (typeof href !== 'string' || !href) {
20452012
console.error(
@@ -2067,6 +2034,9 @@ function prefetchDNS(href: string, options?: mixed) {
20672034
}
20682035
20692036
function preconnect(href: string, options?: {crossOrigin?: string}) {
2037+
if (!enableFloat) {
2038+
return;
2039+
}
20702040
if (__DEV__) {
20712041
if (typeof href !== 'string' || !href) {
20722042
console.error(
@@ -2102,10 +2072,13 @@ type PreloadOptions = {
21022072
type?: string,
21032073
};
21042074
function preload(href: string, options: PreloadOptions) {
2075+
if (!enableFloat) {
2076+
return;
2077+
}
21052078
if (__DEV__) {
21062079
validatePreloadArguments(href, options);
21072080
}
2108-
const ownerDocument = getDocumentForPreloads();
2081+
const ownerDocument = getDocumentForImperativeFloatMethods();
21092082
if (
21102083
typeof href === 'string' &&
21112084
href &&
@@ -2163,61 +2136,25 @@ type PreinitOptions = {
21632136
integrity?: string,
21642137
};
21652138
function preinit(href: string, options: PreinitOptions) {
2139+
if (!enableFloat) {
2140+
return;
2141+
}
21662142
if (__DEV__) {
21672143
validatePreinitArguments(href, options);
21682144
}
2145+
const ownerDocument = getDocumentForImperativeFloatMethods();
21692146
21702147
if (
21712148
typeof href === 'string' &&
21722149
href &&
21732150
typeof options === 'object' &&
21742151
options !== null
21752152
) {
2176-
const resourceRoot = getCurrentResourceRoot();
21772153
const as = options.as;
2178-
if (!resourceRoot) {
2179-
if (as === 'style' || as === 'script') {
2180-
// We are going to emit a preload as a best effort fallback since this preinit
2181-
// was called outside of a render. Given the passive nature of this fallback
2182-
// we do not warn in dev when props disagree if there happens to already be a
2183-
// matching preload with this href
2184-
const preloadDocument = getDocumentForPreloads();
2185-
if (preloadDocument) {
2186-
const limitedEscapedHref =
2187-
escapeSelectorAttributeValueInsideDoubleQuotes(href);
2188-
const preloadKey = `link[rel="preload"][as="${as}"][href="${limitedEscapedHref}"]`;
2189-
let key = preloadKey;
2190-
switch (as) {
2191-
case 'style':
2192-
key = getStyleKey(href);
2193-
break;
2194-
case 'script':
2195-
key = getScriptKey(href);
2196-
break;
2197-
}
2198-
if (!preloadPropsMap.has(key)) {
2199-
const preloadProps = preloadPropsFromPreinitOptions(
2200-
href,
2201-
as,
2202-
options,
2203-
);
2204-
preloadPropsMap.set(key, preloadProps);
2205-
2206-
if (null === preloadDocument.querySelector(preloadKey)) {
2207-
const instance = preloadDocument.createElement('link');
2208-
setInitialProperties(instance, 'link', preloadProps);
2209-
markNodeAsHoistable(instance);
2210-
(preloadDocument.head: any).appendChild(instance);
2211-
}
2212-
}
2213-
}
2214-
}
2215-
return;
2216-
}
22172154
22182155
switch (as) {
22192156
case 'style': {
2220-
const styles = getResourcesFromRoot(resourceRoot).hoistableStyles;
2157+
const styles = getResourcesFromRoot(ownerDocument).hoistableStyles;
22212158
22222159
const key = getStyleKey(href);
22232160
const precedence = options.precedence || 'default';
@@ -2236,7 +2173,7 @@ function preinit(href: string, options: PreinitOptions) {
22362173
};
22372174
22382175
// Attempt to hydrate instance from DOM
2239-
let instance: null | Instance = resourceRoot.querySelector(
2176+
let instance: null | Instance = ownerDocument.querySelector(
22402177
getStylesheetSelectorFromKey(key),
22412178
);
22422179
if (instance) {
@@ -2252,7 +2189,6 @@ function preinit(href: string, options: PreinitOptions) {
22522189
if (preloadProps) {
22532190
adoptPreloadPropsForStylesheet(stylesheetProps, preloadProps);
22542191
}
2255-
const ownerDocument = getDocumentFromRoot(resourceRoot);
22562192
const link = (instance = ownerDocument.createElement('link'));
22572193
markNodeAsHoistable(link);
22582194
setInitialProperties(link, 'link', stylesheetProps);
@@ -2269,7 +2205,7 @@ function preinit(href: string, options: PreinitOptions) {
22692205
});
22702206
22712207
state.loading |= Inserted;
2272-
insertStylesheet(instance, precedence, resourceRoot);
2208+
insertStylesheet(instance, precedence, ownerDocument);
22732209
}
22742210
22752211
// Construct a Resource and cache it
@@ -2284,7 +2220,7 @@ function preinit(href: string, options: PreinitOptions) {
22842220
}
22852221
case 'script': {
22862222
const src = href;
2287-
const scripts = getResourcesFromRoot(resourceRoot).hoistableScripts;
2223+
const scripts = getResourcesFromRoot(ownerDocument).hoistableScripts;
22882224
22892225
const key = getScriptKey(src);
22902226
@@ -2297,7 +2233,7 @@ function preinit(href: string, options: PreinitOptions) {
22972233
}
22982234
22992235
// Attempt to hydrate instance from DOM
2300-
let instance: null | Instance = resourceRoot.querySelector(
2236+
let instance: null | Instance = ownerDocument.querySelector(
23012237
getScriptSelectorFromKey(key),
23022238
);
23032239
if (!instance) {
@@ -2308,7 +2244,6 @@ function preinit(href: string, options: PreinitOptions) {
23082244
if (preloadProps) {
23092245
adoptPreloadPropsForScript(scriptProps, preloadProps);
23102246
}
2311-
const ownerDocument = getDocumentFromRoot(resourceRoot);
23122247
instance = ownerDocument.createElement('script');
23132248
markNodeAsHoistable(instance);
23142249
setInitialProperties(instance, 'link', scriptProps);
@@ -2329,20 +2264,6 @@ function preinit(href: string, options: PreinitOptions) {
23292264
}
23302265
}
23312266
2332-
function preloadPropsFromPreinitOptions(
2333-
href: string,
2334-
as: ResourceType,
2335-
options: PreinitOptions,
2336-
): PreloadProps {
2337-
return {
2338-
href,
2339-
rel: 'preload',
2340-
as,
2341-
crossOrigin: as === 'font' ? '' : options.crossOrigin,
2342-
integrity: options.integrity,
2343-
};
2344-
}
2345-
23462267
function stylesheetPropsFromPreinitOptions(
23472268
href: string,
23482269
precedence: string,

packages/react-dom/src/__tests__/ReactDOMFloat-test.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3831,7 +3831,7 @@ body {
38313831
});
38323832

38333833
// @gate enableFloat
3834-
it('creates a preload resource when ReactDOM.preinit(..., {as: "style" }) is called outside of render on the client', async () => {
3834+
it('creates a stylesheet resource in the ownerDocument when ReactDOM.preinit(..., {as: "style" }) is called outside of render on the client', async () => {
38353835
function App() {
38363836
React.useEffect(() => {
38373837
ReactDOM.preinit('foo', {as: 'style'});
@@ -3849,11 +3849,55 @@ body {
38493849
expect(getMeaningfulChildren(document)).toEqual(
38503850
<html>
38513851
<head>
3852-
<link rel="preload" href="foo" as="style" />
3852+
<link rel="stylesheet" href="foo" data-precedence="default" />
3853+
</head>
3854+
<body>foo</body>
3855+
</html>,
3856+
);
3857+
});
3858+
3859+
// @gate enableFloat
3860+
it('creates a stylesheet resource in the ownerDocument when ReactDOM.preinit(..., {as: "style" }) is called outside of render on the client', async () => {
3861+
// This is testing behavior, but it shows that it is not a good idea to preinit inside a shadowRoot. The point is we are asserting a behavior
3862+
// you would want to avoid in a real app.
3863+
const shadow = document.body.attachShadow({mode: 'open'});
3864+
function ShadowComponent() {
3865+
ReactDOM.preinit('bar', {as: 'style'});
3866+
return null;
3867+
}
3868+
function App() {
3869+
React.useEffect(() => {
3870+
ReactDOM.preinit('foo', {as: 'style'});
3871+
}, []);
3872+
return (
3873+
<html>
3874+
<body>
3875+
foo
3876+
{ReactDOM.createPortal(
3877+
<div>
3878+
<ShadowComponent />
3879+
shadow
3880+
</div>,
3881+
shadow,
3882+
)}
3883+
</body>
3884+
</html>
3885+
);
3886+
}
3887+
3888+
const root = ReactDOMClient.createRoot(document);
3889+
root.render(<App />);
3890+
await waitForAll([]);
3891+
expect(getMeaningfulChildren(document)).toEqual(
3892+
<html>
3893+
<head>
3894+
<link rel="stylesheet" href="bar" data-precedence="default" />
3895+
<link rel="stylesheet" href="foo" data-precedence="default" />
38533896
</head>
38543897
<body>foo</body>
38553898
</html>,
38563899
);
3900+
expect(getMeaningfulChildren(shadow)).toEqual(<div>shadow</div>);
38573901
});
38583902

38593903
// @gate enableFloat
@@ -3953,7 +3997,7 @@ body {
39533997
expect(getMeaningfulChildren(document)).toEqual(
39543998
<html>
39553999
<head>
3956-
<link rel="preload" href="foo" as="script" />
4000+
<script async="" src="foo" />
39574001
</head>
39584002
<body>foo</body>
39594003
</html>,

packages/react-dom/src/client/ReactDOMRoot.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,8 @@ export function createRoot(
247247
transitionCallbacks,
248248
);
249249
markContainerAsRoot(root.current, container);
250+
Dispatcher.current = ReactDOMClientDispatcher;
250251

251-
if (enableFloat) {
252-
// Set the default dispatcher to the client dispatcher
253-
Dispatcher.current = ReactDOMClientDispatcher;
254-
}
255252
const rootContainerElement: Document | Element | DocumentFragment =
256253
container.nodeType === COMMENT_NODE
257254
? (container.parentNode: any)
@@ -339,10 +336,7 @@ export function hydrateRoot(
339336
transitionCallbacks,
340337
);
341338
markContainerAsRoot(root.current, container);
342-
if (enableFloat) {
343-
// Set the default dispatcher to the client dispatcher
344-
Dispatcher.current = ReactDOMClientDispatcher;
345-
}
339+
Dispatcher.current = ReactDOMClientDispatcher;
346340
// This can't be a comment node since hydration doesn't work on comment nodes anyway.
347341
listenToAllSupportedEvents(container);
348342

packages/react-native-renderer/src/ReactFabricHostConfig.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,3 @@ export function suspendInstance(type: Type, props: Props): void {}
466466
export function waitForCommitToBeReady(): null {
467467
return null;
468468
}
469-
470-
export function prepareRendererToRender(container: Container): void {
471-
// noop
472-
}
473-
474-
export function resetRendererAfterRender() {
475-
// noop
476-
}

0 commit comments

Comments
 (0)