Skip to content

Commit

Permalink
[Float][Fizz][Fiber] implement preconnect and prefetchDNS float metho…
Browse files Browse the repository at this point in the history
…ds (#26237)

Adds two new ReactDOM methods

### `ReactDOM.prefetchDNS(href: string)`
In SSR this method will cause a `<link rel="dns-prefetch" href="..." />`
to flush before most other content both on intial flush (Shell) and late
flushes. It will only emit one link per href.

On the client, this method will case the same kind of link to be
inserted into the document immediately (when called during render, not
during commit) if there is not already a matching element in the
document.

### `ReactDOM.preconnect(href: string, options?: { crossOrigin?: string
})`
In SSR this method will cause a `<link rel="dns-prefetch" href="..."
[corssorigin="..."] />` to flush before most other content both on
intial flush (Shell) and late flushes. It will only emit one link per
href + crossorigin combo.

On the client, this method will case the same kind of link to be
inserted into the document immediately (when called during render, not
during commit) if there is not already a matching element in the
document.

DiffTrain build for [1173a17](1173a17)
  • Loading branch information
gnoff committed Feb 25, 2023
1 parent 3b9bf76 commit 81e2051
Show file tree
Hide file tree
Showing 31 changed files with 1,427 additions and 90 deletions.
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e7d7d4cb4b61624c5762521370a58d941c1c46e3
1173a17e6eea08599005e9f8e7d7a4245783edba
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION_TRANSFORMS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e7d7d4cb4b61624c5762521370a58d941c1c46e3
1173a17e6eea08599005e9f8e7d7a4245783edba
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (
}
"use strict";

var ReactVersion = "18.3.0-www-classic-e7d7d4cb4-20230225";
var ReactVersion = "18.3.0-www-classic-1173a17e6-20230225";

// ATTENTION
// When adding new symbols to this file,
Expand Down
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (
}
"use strict";

var ReactVersion = "18.3.0-www-modern-e7d7d4cb4-20230225";
var ReactVersion = "18.3.0-www-modern-1173a17e6-20230225";

// ATTENTION
// When adding new symbols to this file,
Expand Down
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-prod.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,4 +646,4 @@ exports.useSyncExternalStore = function (
);
};
exports.useTransition = useTransition;
exports.version = "18.3.0-www-classic-e7d7d4cb4-20230225";
exports.version = "18.3.0-www-classic-1173a17e6-20230225";
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-prod.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,4 +638,4 @@ exports.useSyncExternalStore = function (
);
};
exports.useTransition = useTransition;
exports.version = "18.3.0-www-modern-e7d7d4cb4-20230225";
exports.version = "18.3.0-www-modern-1173a17e6-20230225";
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-profiling.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ exports.useSyncExternalStore = function (
);
};
exports.useTransition = useTransition;
exports.version = "18.3.0-www-classic-e7d7d4cb4-20230225";
exports.version = "18.3.0-www-classic-1173a17e6-20230225";

/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
if (
Expand Down
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-profiling.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ exports.useSyncExternalStore = function (
);
};
exports.useTransition = useTransition;
exports.version = "18.3.0-www-modern-e7d7d4cb4-20230225";
exports.version = "18.3.0-www-modern-1173a17e6-20230225";

/* global __REACT_DEVTOOLS_GLOBAL_HOOK__ */
if (
Expand Down
2 changes: 1 addition & 1 deletion compiled/facebook-www/ReactART-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function _assertThisInitialized(self) {
return self;
}

var ReactVersion = "18.3.0-www-classic-e7d7d4cb4-20230225";
var ReactVersion = "18.3.0-www-classic-1173a17e6-20230225";

var LegacyRoot = 0;
var ConcurrentRoot = 1;
Expand Down
2 changes: 1 addition & 1 deletion compiled/facebook-www/ReactART-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function _assertThisInitialized(self) {
return self;
}

var ReactVersion = "18.3.0-www-modern-e7d7d4cb4-20230225";
var ReactVersion = "18.3.0-www-modern-1173a17e6-20230225";

var LegacyRoot = 0;
var ConcurrentRoot = 1;
Expand Down
4 changes: 2 additions & 2 deletions compiled/facebook-www/ReactART-prod.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -9780,7 +9780,7 @@ var slice = Array.prototype.slice,
return null;
},
bundleType: 0,
version: "18.3.0-www-classic-e7d7d4cb4-20230225",
version: "18.3.0-www-classic-1173a17e6-20230225",
rendererPackageName: "react-art"
};
var internals$jscomp$inline_1300 = {
Expand Down Expand Up @@ -9811,7 +9811,7 @@ var internals$jscomp$inline_1300 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-next-e7d7d4cb4-20230225"
reconcilerVersion: "18.3.0-next-1173a17e6-20230225"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1301 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
4 changes: 2 additions & 2 deletions compiled/facebook-www/ReactART-prod.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -9445,7 +9445,7 @@ var slice = Array.prototype.slice,
return null;
},
bundleType: 0,
version: "18.3.0-www-modern-e7d7d4cb4-20230225",
version: "18.3.0-www-modern-1173a17e6-20230225",
rendererPackageName: "react-art"
};
var internals$jscomp$inline_1280 = {
Expand Down Expand Up @@ -9476,7 +9476,7 @@ var internals$jscomp$inline_1280 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-next-e7d7d4cb4-20230225"
reconcilerVersion: "18.3.0-next-1173a17e6-20230225"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1281 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
133 changes: 126 additions & 7 deletions compiled/facebook-www/ReactDOM-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -9403,11 +9403,14 @@ function cleanupAfterRenderResources() {
// from Internals -> ReactDOM -> FloatClient -> Internals so this doesn't introduce a new one.

var ReactDOMClientDispatcher = {
prefetchDNS: prefetchDNS$1,
preconnect: preconnect$1,
preload: preload$1,
preinit: preinit$1
}; // global maps of Resources
}; // global collections of Resources

var preloadPropsMap = new Map(); // getRootNode is missing from IE and old jsdom versions
var preloadPropsMap = new Map();
var preconnectsSet = new Set(); // getRootNode is missing from IE and old jsdom versions

function getHoistableRoot(container) {
// $FlowFixMe[method-unbinding]
Expand Down Expand Up @@ -9444,8 +9447,104 @@ function getDocumentForPreloads() {

function getDocumentFromRoot(root) {
return root.ownerDocument || root;
}

function preconnectAs(rel, crossOrigin, href) {
var ownerDocument = getDocumentForPreloads();

if (typeof href === "string" && href && ownerDocument) {
var limitedEscapedHref =
escapeSelectorAttributeValueInsideDoubleQuotes(href);
var key = 'link[rel="' + rel + '"][href="' + limitedEscapedHref + '"]';

if (typeof crossOrigin === "string") {
key += '[crossorigin="' + crossOrigin + '"]';
}

if (!preconnectsSet.has(key)) {
preconnectsSet.add(key);
var preconnectProps = {
rel: rel,
crossOrigin: crossOrigin,
href: href
};

if (null === ownerDocument.querySelector(key)) {
var preloadInstance = createElement(
"link",
preconnectProps,
ownerDocument,
HTML_NAMESPACE
);
setInitialProperties(preloadInstance, "link", preconnectProps);
markNodeAsResource(preloadInstance);
ownerDocument.head.appendChild(preloadInstance);
}
}
}
} // --------------------------------------
// ReactDOM.prefetchDNS
// --------------------------------------

function prefetchDNS$1(href, options) {
{
if (typeof href !== "string" || !href) {
error(
"ReactDOM.prefetchDNS(): Expected the `href` argument (first) to be a non-empty string but encountered %s instead.",
getValueDescriptorExpectingObjectForWarning(href)
);
} else if (options != null) {
if (
typeof options === "object" &&
options.hasOwnProperty("crossOrigin")
) {
error(
"ReactDOM.prefetchDNS(): Expected only one argument, `href`, but encountered %s as a second argument instead. This argument is reserved for future options and is currently disallowed. It looks like the you are attempting to set a crossOrigin property for this DNS lookup hint. Browsers do not perform DNS queries using CORS and setting this attribute on the resource hint has no effect. Try calling ReactDOM.prefetchDNS() with just a single string argument, `href`.",
getValueDescriptorExpectingEnumForWarning(options)
);
} else {
error(
"ReactDOM.prefetchDNS(): Expected only one argument, `href`, but encountered %s as a second argument instead. This argument is reserved for future options and is currently disallowed. Try calling ReactDOM.prefetchDNS() with just a single string argument, `href`.",
getValueDescriptorExpectingEnumForWarning(options)
);
}
}
}

preconnectAs("dns-prefetch", null, href);
} // --------------------------------------
// ReactDOM.preconnect
// --------------------------------------

function preconnect$1(href, options) {
{
if (typeof href !== "string" || !href) {
error(
"ReactDOM.preconnect(): Expected the `href` argument (first) to be a non-empty string but encountered %s instead.",
getValueDescriptorExpectingObjectForWarning(href)
);
} else if (options != null && typeof options !== "object") {
error(
"ReactDOM.preconnect(): Expected the `options` argument (second) to be an object but encountered %s instead. The only supported option at this time is `crossOrigin` which accepts a string.",
getValueDescriptorExpectingEnumForWarning(options)
);
} else if (options != null && typeof options.crossOrigin !== "string") {
error(
"ReactDOM.preconnect(): Expected the `crossOrigin` option (second argument) to be a string but encountered %s instead. Try removing this option or passing a string value instead.",
getValueDescriptorExpectingObjectForWarning(options.crossOrigin)
);
}
}

var crossOrigin =
options == null || typeof options.crossOrigin !== "string"
? null
: options.crossOrigin === "use-credentials"
? "use-credentials"
: "";
preconnectAs("preconnect", crossOrigin, href);
} // --------------------------------------
// ReactDOM.Preload
// ReactDOM.preload
// --------------------------------------

function preload$1(href, options) {
Expand Down Expand Up @@ -42002,7 +42101,7 @@ function createFiberRoot(
return root;
}

var ReactVersion = "18.3.0-www-classic-e7d7d4cb4-20230225";
var ReactVersion = "18.3.0-www-classic-1173a17e6-20230225";

function createPortal$1(
children,
Expand Down Expand Up @@ -43486,12 +43585,21 @@ function createEventHandle(type, options) {
}
}

function preinit() {
function prefetchDNS() {
var dispatcher = Internals.Dispatcher.current;

if (dispatcher) {
dispatcher.preinit.apply(this, arguments);
} // We don't error because preinit needs to be resilient to being called in a variety of scopes
dispatcher.prefetchDNS.apply(this, arguments);
} // We don't error because preconnect needs to be resilient to being called in a variety of scopes
// and the runtime may not be capable of responding. The function is optimistic and not critical
// so we favor silent bailout over warning or erroring.
}
function preconnect() {
var dispatcher = Internals.Dispatcher.current;

if (dispatcher) {
dispatcher.preconnect.apply(this, arguments);
} // We don't error because preconnect needs to be resilient to being called in a variety of scopes
// and the runtime may not be capable of responding. The function is optimistic and not critical
// so we favor silent bailout over warning or erroring.
}
Expand All @@ -43504,6 +43612,15 @@ function preload() {
// and the runtime may not be capable of responding. The function is optimistic and not critical
// so we favor silent bailout over warning or erroring.
}
function preinit() {
var dispatcher = Internals.Dispatcher.current;

if (dispatcher) {
dispatcher.preinit.apply(this, arguments);
} // We don't error because preinit needs to be resilient to being called in a variety of scopes
// and the runtime may not be capable of responding. The function is optimistic and not critical
// so we favor silent bailout over warning or erroring.
}

setAttemptSynchronousHydration(attemptSynchronousHydration);
setAttemptDiscreteHydration(attemptDiscreteHydration);
Expand Down Expand Up @@ -43657,6 +43774,8 @@ exports.findDOMNode = findDOMNode;
exports.flushSync = flushSync;
exports.hydrate = hydrate;
exports.hydrateRoot = hydrateRoot;
exports.preconnect = preconnect;
exports.prefetchDNS = prefetchDNS;
exports.preinit = preinit;
exports.preload = preload;
exports.render = render;
Expand Down
Loading

0 comments on commit 81e2051

Please sign in to comment.