Skip to content

Commit

Permalink
[Float][Fizz][Fiber] support imagesrcset and imagesizes for `ReactDOM…
Browse files Browse the repository at this point in the history
….preload()` (#26940)

For float methods the href argument is usually all we need to uniquely
key the request. However when preloading responsive images it is
possible that you may need more than one preload for differing
imagesizes attributes. When using imagesrcset for preloads the href
attribute acts more like a fallback href. For keying purposes the
imagesrcset becomes the primary key conceptually.

This change updates the keying logic for `ReactDOM.preload()` when you
pass `{as: "image"}`

1. If `options.imageSrcSet` is a non-emtpy string the key is defined as
`options.imageSrcSet + options.imageSizes`. The `href` argument is still
required but does not participate in keying.
2. If `options.imageSrcSet` is empty, missing, or an invalid format the
key is defined as the `href`. Changing the `options.imageSizes` does not
affect the key as this option is inert when not using
`options.imageSrcSet`

Additionally, currently there is a bug in webkit (Safari) that causes
preload links to fail to use imageSrcSet and fallback to href even when
the browser will correctly resolve srcset on an `<img>` tag. Because the
drawbacks of preloading the wrong image (href over imagesrcset) in a
modern browser outweight the drawbacks of not preloading anything for
responsive images in browsers that do not support srcset at all we will
omit the `href` attribute whenever `options.imageSrcSet` is provided. We
still require you provide an href since we want to be able to revert
this behavior once all major browsers support it

bug link: https://bugs.webkit.org/show_bug.cgi?id=231150

DiffTrain build for [fc929cf](fc929cf)
  • Loading branch information
gnoff committed Jun 15, 2023
1 parent b437e4d commit fc485c3
Show file tree
Hide file tree
Showing 22 changed files with 807 additions and 487 deletions.
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION
@@ -1 +1 @@
86acc10f2596e1a6fe2fd57a5b325de85175800b
fc929cf4ead35f99c4e9612a95e8a0bb8f5df25d
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-dev.modern.js
Expand Up @@ -27,7 +27,7 @@ if (
}
"use strict";

var ReactVersion = "18.3.0-www-modern-5fb44b73";
var ReactVersion = "18.3.0-www-modern-58022814";

// ATTENTION
// When adding new symbols to this file,
Expand Down
2 changes: 1 addition & 1 deletion compiled/facebook-www/ReactART-dev.modern.js
Expand Up @@ -69,7 +69,7 @@ function _assertThisInitialized(self) {
return self;
}

var ReactVersion = "18.3.0-www-modern-520e6c62";
var ReactVersion = "18.3.0-www-modern-c8bee3bc";

var LegacyRoot = 0;
var ConcurrentRoot = 1;
Expand Down
4 changes: 2 additions & 2 deletions compiled/facebook-www/ReactART-prod.modern.js
Expand Up @@ -9858,7 +9858,7 @@ var slice = Array.prototype.slice,
return null;
},
bundleType: 0,
version: "18.3.0-www-modern-e8821afc",
version: "18.3.0-www-modern-12c03e2f",
rendererPackageName: "react-art"
};
var internals$jscomp$inline_1298 = {
Expand Down Expand Up @@ -9889,7 +9889,7 @@ var internals$jscomp$inline_1298 = {
scheduleRoot: null,
setRefreshHandler: null,
getCurrentFiber: null,
reconcilerVersion: "18.3.0-www-modern-e8821afc"
reconcilerVersion: "18.3.0-www-modern-12c03e2f"
};
if ("undefined" !== typeof __REACT_DEVTOOLS_GLOBAL_HOOK__) {
var hook$jscomp$inline_1299 = __REACT_DEVTOOLS_GLOBAL_HOOK__;
Expand Down
140 changes: 72 additions & 68 deletions compiled/facebook-www/ReactDOM-dev.classic.js
Expand Up @@ -34178,7 +34178,7 @@ function createFiberRoot(
return root;
}

var ReactVersion = "18.3.0-www-classic-3dccae4a";
var ReactVersion = "18.3.0-www-classic-00cc677f";

function createPortal$1(
children,
Expand Down Expand Up @@ -42068,65 +42068,6 @@ function propNamesListJoin(list, combinator) {
}
}

function validatePreloadArguments(href, options) {
{
if (!href || typeof href !== "string") {
var typeOfArg = getValueDescriptorExpectingObjectForWarning(href);

error(
"ReactDOM.preload() expected the first argument to be a string representing an href but found %s instead.",
typeOfArg
);
} else if (typeof options !== "object" || options === null) {
var _typeOfArg = getValueDescriptorExpectingObjectForWarning(options);

error(
'ReactDOM.preload() expected the second argument to be an options argument containing at least an "as" property' +
' specifying the Resource type. It found %s instead. The href for the preload call where this warning originated is "%s".',
_typeOfArg,
href
);
} else {
var as = options.as;

switch (as) {
// Font specific validation of options
case "font": {
if (options.crossOrigin === "use-credentials") {
error(
'ReactDOM.preload() was called with an "as" type of "font" and with a "crossOrigin" option of "use-credentials".' +
' Fonts preloading must use crossOrigin "anonymous" to be functional. Please update your font preload to omit' +
' the crossOrigin option or change it to any other value than "use-credentials" (Browsers default all other values' +
' to anonymous mode). The href for the preload call where this warning originated is "%s"',
href
);
}

break;
}

case "script":
case "style": {
break;
}
// We have an invalid as type and need to warn

default: {
var typeOfAs = getValueDescriptorExpectingEnumForWarning(as);

error(
'ReactDOM.preload() expected a valid "as" type in the options (second) argument but found %s instead.' +
" Please use one of the following valid values instead: %s. The href for the preload call where this" +
' warning originated is "%s".',
typeOfAs,
'"style", "font", or "script"',
href
);
}
}
}
}
}
function validatePreinitArguments(href, options) {
{
if (!href || typeof href !== "string") {
Expand All @@ -42137,12 +42078,12 @@ function validatePreinitArguments(href, options) {
typeOfArg
);
} else if (typeof options !== "object" || options === null) {
var _typeOfArg2 = getValueDescriptorExpectingObjectForWarning(options);
var _typeOfArg = getValueDescriptorExpectingObjectForWarning(options);

error(
'ReactDOM.preinit() expected the second argument to be an options argument containing at least an "as" property' +
' specifying the Resource type. It found %s instead. The href for the preload call where this warning originated is "%s".',
_typeOfArg2,
_typeOfArg,
href
);
} else {
Expand Down Expand Up @@ -43755,7 +43696,35 @@ function preconnect$1(href, options) {

function preload$1(href, options) {
{
validatePreloadArguments(href, options);
// TODO move this to ReactDOMFloat and expose a stricter function interface or possibly
// typed functions (preloadImage, preloadStyle, ...)
var encountered = "";

if (typeof href !== "string" || !href) {
encountered +=
"The `href` argument encountered was " +
getValueDescriptorExpectingObjectForWarning(href) +
".";
}

if (options == null || typeof options !== "object") {
encountered +=
"The `options` argument encountered was " +
getValueDescriptorExpectingObjectForWarning(options) +
".";
} else if (typeof options.as !== "string" || !options.as) {
encountered +=
"The `as` option encountered was " +
getValueDescriptorExpectingObjectForWarning(options.as) +
".";
}

if (encountered) {
error(
'ReactDOM.preload(): Expected two arguments, a non-empty `href` string and an `options` object with an `as` property valid for a `<link rel="preload" as="..." />` tag. %s',
encountered
);
}
}

var ownerDocument = getDocumentForImperativeFloatMethods();
Expand All @@ -43765,13 +43734,42 @@ function preload$1(href, options) {
href &&
typeof options === "object" &&
options !== null &&
typeof options.as === "string" &&
options.as &&
ownerDocument
) {
var as = options.as;
var limitedEscapedHref =
escapeSelectorAttributeValueInsideDoubleQuotes(href);
var preloadSelector =
'link[rel="preload"][as="' + as + '"][href="' + limitedEscapedHref + '"]'; // Some preloads are keyed under their selector. This happens when the preload is for
'link[rel="preload"][as="' +
escapeSelectorAttributeValueInsideDoubleQuotes(as) +
'"]';

if (as === "image") {
var imageSrcSet = options.imageSrcSet,
imageSizes = options.imageSizes;

if (typeof imageSrcSet === "string" && imageSrcSet !== "") {
preloadSelector +=
'[imagesrcset="' +
escapeSelectorAttributeValueInsideDoubleQuotes(imageSrcSet) +
'"]';

if (typeof imageSizes === "string") {
preloadSelector +=
'[imagesizes="' +
escapeSelectorAttributeValueInsideDoubleQuotes(imageSizes) +
'"]';
}
} else {
preloadSelector +=
'[href="' +
escapeSelectorAttributeValueInsideDoubleQuotes(href) +
'"]';
}
} else {
preloadSelector +=
'[href="' + escapeSelectorAttributeValueInsideDoubleQuotes(href) + '"]';
} // Some preloads are keyed under their selector. This happens when the preload is for
// an arbitrary type. Other preloads are keyed under the resource key they represent a preload for.
// Here we figure out which key to use to determine if we have a preload already.

Expand Down Expand Up @@ -43817,14 +43815,20 @@ function preload$1(href, options) {

function preloadPropsFromPreloadOptions(href, as, options) {
return {
href: href,
rel: "preload",
as: as,
// There is a bug in Safari where imageSrcSet is not respected on preload links
// so we omit the href here if we have imageSrcSet b/c safari will load the wrong image.
// This harms older browers that do not support imageSrcSet by making their preloads not work
// but this population is shrinking fast and is already small so we accept this tradeoff.
href: as === "image" && options.imageSrcSet ? undefined : href,
crossOrigin: as === "font" ? "" : options.crossOrigin,
integrity: options.integrity,
type: options.type,
nonce: options.nonce,
fetchPriority: options.fetchPriority
fetchPriority: options.fetchPriority,
imageSrcSet: options.imageSrcSet,
imageSizes: options.imageSizes
};
}

Expand Down

0 comments on commit fc485c3

Please sign in to comment.