Skip to content

Commit ea7881e

Browse files
committed
[Fizz] preload bootstrapModules (#26754)
stacked on #26753 Adds support for preloading bootstrapModules. We don't yet support modules in Float's public interface but this implementation should be compatible with what we do when we add it. DiffTrain build for [ae31d2e](ae31d2e)
1 parent cea3a73 commit ea7881e

8 files changed

+170
-79
lines changed

compiled/facebook-www/REVISION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b864ad4397e7b366ece9ecfdfafa5660ae6b8390
1+
ae31d2ea3c3f9f0a87ff2c6193484d5d8786bc5f

compiled/facebook-www/ReactDOMServer-dev.classic.js

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if (__DEV__) {
1919
var React = require("react");
2020
var ReactDOM = require("react-dom");
2121

22-
var ReactVersion = "18.3.0-www-classic-b863116e";
22+
var ReactVersion = "18.3.0-www-classic-d2e90a0e";
2323

2424
// This refers to a WWW module.
2525
var warningWWW = require("warning");
@@ -2498,6 +2498,7 @@ function createResponseState$1(
24982498
var _integrity =
24992499
typeof _scriptConfig === "string" ? undefined : _scriptConfig.integrity;
25002500

2501+
preloadBootstrapModule(resources, _src, nonce, _integrity);
25012502
bootstrapChunks.push(
25022503
startModuleSrc,
25032504
stringToChunk(escapeTextForBrowser(_src))
@@ -4083,7 +4084,7 @@ function pushLink(
40834084
}
40844085

40854086
pushLinkImpl(resource.chunks, resource.props);
4086-
resources.usedStylesheets.add(resource);
4087+
resources.usedStylesheets.set(key, resource);
40874088
return pushLinkImpl(target, props);
40884089
} else {
40894090
// This stylesheet refers to a Resource and we create a new one if necessary
@@ -6297,9 +6298,7 @@ function writePreamble(
62976298
resources.fontPreloads.clear(); // Flush unblocked stylesheets by precedence
62986299

62996300
resources.precedences.forEach(flushAllStylesInPreamble, destination);
6300-
resources.usedStylesheets.forEach(function (resource) {
6301-
var key = getResourceKey(resource.props.as, resource.props.href);
6302-
6301+
resources.usedStylesheets.forEach(function (resource, key) {
63036302
if (resources.stylesMap.has(key));
63046303
else {
63056304
var _chunks = resource.chunks;
@@ -6377,9 +6376,7 @@ function writeHoistables(destination, resources, responseState) {
63776376
// but we want to kick off preloading as soon as possible
63786377

63796378
resources.precedences.forEach(preloadLateStyles, destination);
6380-
resources.usedStylesheets.forEach(function (resource) {
6381-
var key = getResourceKey(resource.props.as, resource.props.href);
6382-
6379+
resources.usedStylesheets.forEach(function (resource, key) {
63836380
if (resources.stylesMap.has(key));
63846381
else {
63856382
var chunks = resource.chunks;
@@ -6881,7 +6878,7 @@ function createResources() {
68816878
// usedImagePreloads: new Set(),
68826879
precedences: new Map(),
68836880
stylePrecedences: new Map(),
6884-
usedStylesheets: new Set(),
6881+
usedStylesheets: new Map(),
68856882
scripts: new Set(),
68866883
usedScripts: new Set(),
68876884
explicitStylesheetPreloads: new Set(),
@@ -7505,6 +7502,42 @@ function preloadBootstrapScript(resources, src, nonce, integrity) {
75057502
resources.preloadsMap.set(key, resource);
75067503
resources.explicitScriptPreloads.add(resource);
75077504
pushLinkImpl(resource.chunks, props);
7505+
} // This function is only safe to call at Request start time since it assumes
7506+
// that each module has not already been preloaded. If we find a need to preload
7507+
// scripts at any other point in time we will need to check whether the preload
7508+
// already exists and not assume it
7509+
7510+
function preloadBootstrapModule(resources, src, nonce, integrity) {
7511+
var key = getResourceKey("script", src);
7512+
7513+
{
7514+
if (resources.preloadsMap.has(key)) {
7515+
// This is coded as a React error because it should be impossible for a userspace preload to preempt this call
7516+
// If a userspace preload can preempt it then this assumption is broken and we need to reconsider this strategy
7517+
// rather than instruct the user to not preload their bootstrap scripts themselves
7518+
error(
7519+
'Internal React Error: React expected bootstrap module with src "%s" to not have been preloaded already. please file an issue',
7520+
src
7521+
);
7522+
}
7523+
}
7524+
7525+
var props = {
7526+
rel: "modulepreload",
7527+
href: src,
7528+
nonce: nonce,
7529+
integrity: integrity
7530+
};
7531+
var resource = {
7532+
type: "preload",
7533+
chunks: [],
7534+
state: NoState,
7535+
props: props
7536+
};
7537+
resources.preloadsMap.set(key, resource);
7538+
resources.explicitScriptPreloads.add(resource);
7539+
pushLinkImpl(resource.chunks, props);
7540+
return;
75087541
}
75097542

75107543
function internalPreinitScript(resources, src, chunks) {

compiled/facebook-www/ReactDOMServer-dev.modern.js

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if (__DEV__) {
1919
var React = require("react");
2020
var ReactDOM = require("react-dom");
2121

22-
var ReactVersion = "18.3.0-www-modern-f30fea22";
22+
var ReactVersion = "18.3.0-www-modern-341d0376";
2323

2424
// This refers to a WWW module.
2525
var warningWWW = require("warning");
@@ -2498,6 +2498,7 @@ function createResponseState$1(
24982498
var _integrity =
24992499
typeof _scriptConfig === "string" ? undefined : _scriptConfig.integrity;
25002500

2501+
preloadBootstrapModule(resources, _src, nonce, _integrity);
25012502
bootstrapChunks.push(
25022503
startModuleSrc,
25032504
stringToChunk(escapeTextForBrowser(_src))
@@ -4083,7 +4084,7 @@ function pushLink(
40834084
}
40844085

40854086
pushLinkImpl(resource.chunks, resource.props);
4086-
resources.usedStylesheets.add(resource);
4087+
resources.usedStylesheets.set(key, resource);
40874088
return pushLinkImpl(target, props);
40884089
} else {
40894090
// This stylesheet refers to a Resource and we create a new one if necessary
@@ -6297,9 +6298,7 @@ function writePreamble(
62976298
resources.fontPreloads.clear(); // Flush unblocked stylesheets by precedence
62986299

62996300
resources.precedences.forEach(flushAllStylesInPreamble, destination);
6300-
resources.usedStylesheets.forEach(function (resource) {
6301-
var key = getResourceKey(resource.props.as, resource.props.href);
6302-
6301+
resources.usedStylesheets.forEach(function (resource, key) {
63036302
if (resources.stylesMap.has(key));
63046303
else {
63056304
var _chunks = resource.chunks;
@@ -6377,9 +6376,7 @@ function writeHoistables(destination, resources, responseState) {
63776376
// but we want to kick off preloading as soon as possible
63786377

63796378
resources.precedences.forEach(preloadLateStyles, destination);
6380-
resources.usedStylesheets.forEach(function (resource) {
6381-
var key = getResourceKey(resource.props.as, resource.props.href);
6382-
6379+
resources.usedStylesheets.forEach(function (resource, key) {
63836380
if (resources.stylesMap.has(key));
63846381
else {
63856382
var chunks = resource.chunks;
@@ -6881,7 +6878,7 @@ function createResources() {
68816878
// usedImagePreloads: new Set(),
68826879
precedences: new Map(),
68836880
stylePrecedences: new Map(),
6884-
usedStylesheets: new Set(),
6881+
usedStylesheets: new Map(),
68856882
scripts: new Set(),
68866883
usedScripts: new Set(),
68876884
explicitStylesheetPreloads: new Set(),
@@ -7505,6 +7502,42 @@ function preloadBootstrapScript(resources, src, nonce, integrity) {
75057502
resources.preloadsMap.set(key, resource);
75067503
resources.explicitScriptPreloads.add(resource);
75077504
pushLinkImpl(resource.chunks, props);
7505+
} // This function is only safe to call at Request start time since it assumes
7506+
// that each module has not already been preloaded. If we find a need to preload
7507+
// scripts at any other point in time we will need to check whether the preload
7508+
// already exists and not assume it
7509+
7510+
function preloadBootstrapModule(resources, src, nonce, integrity) {
7511+
var key = getResourceKey("script", src);
7512+
7513+
{
7514+
if (resources.preloadsMap.has(key)) {
7515+
// This is coded as a React error because it should be impossible for a userspace preload to preempt this call
7516+
// If a userspace preload can preempt it then this assumption is broken and we need to reconsider this strategy
7517+
// rather than instruct the user to not preload their bootstrap scripts themselves
7518+
error(
7519+
'Internal React Error: React expected bootstrap module with src "%s" to not have been preloaded already. please file an issue',
7520+
src
7521+
);
7522+
}
7523+
}
7524+
7525+
var props = {
7526+
rel: "modulepreload",
7527+
href: src,
7528+
nonce: nonce,
7529+
integrity: integrity
7530+
};
7531+
var resource = {
7532+
type: "preload",
7533+
chunks: [],
7534+
state: NoState,
7535+
props: props
7536+
};
7537+
resources.preloadsMap.set(key, resource);
7538+
resources.explicitScriptPreloads.add(resource);
7539+
pushLinkImpl(resource.chunks, props);
7540+
return;
75087541
}
75097542

75107543
function internalPreinitScript(resources, src, chunks) {

compiled/facebook-www/ReactDOMServer-prod.classic.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ function pushLink(
538538
}),
539539
resources.preloadsMap.set(responseState, textEmbedded)),
540540
pushLinkImpl(textEmbedded.chunks, textEmbedded.props),
541-
resources.usedStylesheets.add(textEmbedded),
541+
resources.usedStylesheets.set(responseState, textEmbedded),
542542
pushLinkImpl(target, props)
543543
);
544544
href = resources.stylesMap.get(responseState);
@@ -1666,12 +1666,8 @@ function writePreamble(
16661666
resources.fontPreloads.forEach(flushResourceInPreamble, destination);
16671667
resources.fontPreloads.clear();
16681668
resources.precedences.forEach(flushAllStylesInPreamble, destination);
1669-
resources.usedStylesheets.forEach(function (resource) {
1670-
if (
1671-
!resources.stylesMap.has(
1672-
"[" + resource.props.as + "]" + resource.props.href
1673-
)
1674-
)
1669+
resources.usedStylesheets.forEach(function (resource, key) {
1670+
if (!resources.stylesMap.has(key))
16751671
for (resource = resource.chunks, i = 0; i < resource.length; i++)
16761672
destination.push(resource[i]);
16771673
});
@@ -1713,12 +1709,8 @@ function writeHoistables(destination, resources, responseState) {
17131709
resources.fontPreloads.forEach(flushResourceLate, destination);
17141710
resources.fontPreloads.clear();
17151711
resources.precedences.forEach(preloadLateStyles, destination);
1716-
resources.usedStylesheets.forEach(function (resource) {
1717-
if (
1718-
!resources.stylesMap.has(
1719-
"[" + resource.props.as + "]" + resource.props.href
1720-
)
1721-
)
1712+
resources.usedStylesheets.forEach(function (resource, key) {
1713+
if (!resources.stylesMap.has(key))
17221714
for (resource = resource.chunks, i = 0; i < resource.length; i++)
17231715
destination.push(resource[i]);
17241716
});
@@ -3949,7 +3941,7 @@ function renderToStringImpl(
39493941
fontPreloads: new Set(),
39503942
precedences: new Map(),
39513943
stylePrecedences: new Map(),
3952-
usedStylesheets: new Set(),
3944+
usedStylesheets: new Map(),
39533945
scripts: new Set(),
39543946
usedScripts: new Set(),
39553947
explicitStylesheetPreloads: new Set(),
@@ -4016,4 +4008,4 @@ exports.renderToString = function (children, options) {
40164008
'The server used "renderToString" which does not support Suspense. If you intended for this Suspense boundary to render the fallback content on the server consider throwing an Error somewhere within the Suspense boundary. If you intended to have the server wait for the suspended component please switch to "renderToReadableStream" which supports Suspense on the server'
40174009
);
40184010
};
4019-
exports.version = "18.3.0-www-classic-0354aaeb";
4011+
exports.version = "18.3.0-www-classic-bb704388";

compiled/facebook-www/ReactDOMServer-prod.modern.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ function pushLink(
537537
}),
538538
resources.preloadsMap.set(responseState, textEmbedded)),
539539
pushLinkImpl(textEmbedded.chunks, textEmbedded.props),
540-
resources.usedStylesheets.add(textEmbedded),
540+
resources.usedStylesheets.set(responseState, textEmbedded),
541541
pushLinkImpl(target, props)
542542
);
543543
href = resources.stylesMap.get(responseState);
@@ -1665,12 +1665,8 @@ function writePreamble(
16651665
resources.fontPreloads.forEach(flushResourceInPreamble, destination);
16661666
resources.fontPreloads.clear();
16671667
resources.precedences.forEach(flushAllStylesInPreamble, destination);
1668-
resources.usedStylesheets.forEach(function (resource) {
1669-
if (
1670-
!resources.stylesMap.has(
1671-
"[" + resource.props.as + "]" + resource.props.href
1672-
)
1673-
)
1668+
resources.usedStylesheets.forEach(function (resource, key) {
1669+
if (!resources.stylesMap.has(key))
16741670
for (resource = resource.chunks, i = 0; i < resource.length; i++)
16751671
destination.push(resource[i]);
16761672
});
@@ -1712,12 +1708,8 @@ function writeHoistables(destination, resources, responseState) {
17121708
resources.fontPreloads.forEach(flushResourceLate, destination);
17131709
resources.fontPreloads.clear();
17141710
resources.precedences.forEach(preloadLateStyles, destination);
1715-
resources.usedStylesheets.forEach(function (resource) {
1716-
if (
1717-
!resources.stylesMap.has(
1718-
"[" + resource.props.as + "]" + resource.props.href
1719-
)
1720-
)
1711+
resources.usedStylesheets.forEach(function (resource, key) {
1712+
if (!resources.stylesMap.has(key))
17211713
for (resource = resource.chunks, i = 0; i < resource.length; i++)
17221714
destination.push(resource[i]);
17231715
});
@@ -3847,7 +3839,7 @@ function renderToStringImpl(
38473839
fontPreloads: new Set(),
38483840
precedences: new Map(),
38493841
stylePrecedences: new Map(),
3850-
usedStylesheets: new Set(),
3842+
usedStylesheets: new Map(),
38513843
scripts: new Set(),
38523844
usedScripts: new Set(),
38533845
explicitStylesheetPreloads: new Set(),
@@ -3914,4 +3906,4 @@ exports.renderToString = function (children, options) {
39143906
'The server used "renderToString" which does not support Suspense. If you intended for this Suspense boundary to render the fallback content on the server consider throwing an Error somewhere within the Suspense boundary. If you intended to have the server wait for the suspended component please switch to "renderToReadableStream" which supports Suspense on the server'
39153907
);
39163908
};
3917-
exports.version = "18.3.0-www-modern-d90171db";
3909+
exports.version = "18.3.0-www-modern-7ad5a939";

0 commit comments

Comments
 (0)