Skip to content

Commit cea3a73

Browse files
committed
[Fizz] preload bootstrapScripts (#26753)
This PR adds a preload for bootstrapScripts. preloads are captured synchronously when you create a new Request and as such the normal logic to check if a preload already exists is skipped. DiffTrain build for [b864ad4](b864ad4)
1 parent 4d066bf commit cea3a73

8 files changed

+280
-126
lines changed

compiled/facebook-www/REVISION

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

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

Lines changed: 51 additions & 3 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-4ad4dc38";
22+
var ReactVersion = "18.3.0-www-classic-b863116e";
2323

2424
// This refers to a WWW module.
2525
var warningWWW = require("warning");
@@ -2400,6 +2400,7 @@ var scriptReplacer = function (match, prefix, s, suffix) {
24002400
// is set, the server will send instructions via data attributes (instead of inline scripts)
24012401

24022402
function createResponseState$1(
2403+
resources,
24032404
identifierPrefix,
24042405
nonce,
24052406
bootstrapScriptContent,
@@ -2463,6 +2464,7 @@ function createResponseState$1(
24632464
typeof scriptConfig === "string" ? scriptConfig : scriptConfig.src;
24642465
var integrity =
24652466
typeof scriptConfig === "string" ? undefined : scriptConfig.integrity;
2467+
preloadBootstrapScript(resources, src, nonce, integrity);
24662468
bootstrapChunks.push(
24672469
startScriptSrc,
24682470
stringToChunk(escapeTextForBrowser(src))
@@ -7467,6 +7469,42 @@ function preinit(href, options) {
74677469
}
74687470
}
74697471
}
7472+
} // This function is only safe to call at Request start time since it assumes
7473+
// that each script has not already been preloaded. If we find a need to preload
7474+
// scripts at any other point in time we will need to check whether the preload
7475+
// already exists and not assume it
7476+
7477+
function preloadBootstrapScript(resources, src, nonce, integrity) {
7478+
var key = getResourceKey("script", src);
7479+
7480+
{
7481+
if (resources.preloadsMap.has(key)) {
7482+
// This is coded as a React error because it should be impossible for a userspace preload to preempt this call
7483+
// If a userspace preload can preempt it then this assumption is broken and we need to reconsider this strategy
7484+
// rather than instruct the user to not preload their bootstrap scripts themselves
7485+
error(
7486+
'Internal React Error: React expected bootstrap script with src "%s" to not have been preloaded already. please file an issue',
7487+
src
7488+
);
7489+
}
7490+
}
7491+
7492+
var props = {
7493+
rel: "preload",
7494+
href: src,
7495+
as: "script",
7496+
nonce: nonce,
7497+
integrity: integrity
7498+
};
7499+
var resource = {
7500+
type: "preload",
7501+
chunks: [],
7502+
state: NoState,
7503+
props: props
7504+
};
7505+
resources.preloadsMap.set(key, resource);
7506+
resources.explicitScriptPreloads.add(resource);
7507+
pushLinkImpl(resource.chunks, props);
74707508
}
74717509

74727510
function internalPreinitScript(resources, src, chunks) {
@@ -7641,11 +7679,13 @@ function getAsResourceDEV(resource) {
76417679
}
76427680

76437681
function createResponseState(
7682+
resources,
76447683
generateStaticMarkup,
76457684
identifierPrefix,
76467685
externalRuntimeConfig
76477686
) {
76487687
var responseState = createResponseState$1(
7688+
resources,
76497689
identifierPrefix,
76507690
undefined,
76517691
undefined,
@@ -10313,6 +10353,7 @@ function noop() {}
1031310353

1031410354
function createRequest(
1031510355
children,
10356+
resources,
1031610357
responseState,
1031710358
rootFormatContext,
1031810359
progressiveChunkSize,
@@ -10325,7 +10366,6 @@ function createRequest(
1032510366
prepareHostDispatcher();
1032610367
var pingedTasks = [];
1032710368
var abortSet = new Set();
10328-
var resources = createResources();
1032910369
var request = {
1033010370
destination: null,
1033110371
flushScheduled: false,
@@ -12267,7 +12307,12 @@ function flushCompletedQueues(request, destination) {
1226712307
// We haven't flushed the root yet so we don't need to check any other branches further down
1226812308
return;
1226912309
}
12270-
} else if (enableFloat) {
12310+
} else if (request.pendingRootTasks > 0) {
12311+
// We have not yet flushed the root segment so we early return
12312+
return;
12313+
}
12314+
12315+
if (enableFloat) {
1227112316
writeHoistables(destination, request.resources, request.responseState);
1227212317
} // We emit client rendering instructions for already emitted boundaries first.
1227312318
// This is so that we can signal to the client to start client rendering them as
@@ -12485,9 +12530,12 @@ function renderToStringImpl(
1248512530
readyToStream = true;
1248612531
}
1248712532

12533+
var resources = createResources();
1248812534
var request = createRequest(
1248912535
children,
12536+
resources,
1249012537
createResponseState(
12538+
resources,
1249112539
generateStaticMarkup,
1249212540
options ? options.identifierPrefix : undefined,
1249312541
unstable_externalRuntimeSrc

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

Lines changed: 51 additions & 3 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-bcd77572";
22+
var ReactVersion = "18.3.0-www-modern-f30fea22";
2323

2424
// This refers to a WWW module.
2525
var warningWWW = require("warning");
@@ -2400,6 +2400,7 @@ var scriptReplacer = function (match, prefix, s, suffix) {
24002400
// is set, the server will send instructions via data attributes (instead of inline scripts)
24012401

24022402
function createResponseState$1(
2403+
resources,
24032404
identifierPrefix,
24042405
nonce,
24052406
bootstrapScriptContent,
@@ -2463,6 +2464,7 @@ function createResponseState$1(
24632464
typeof scriptConfig === "string" ? scriptConfig : scriptConfig.src;
24642465
var integrity =
24652466
typeof scriptConfig === "string" ? undefined : scriptConfig.integrity;
2467+
preloadBootstrapScript(resources, src, nonce, integrity);
24662468
bootstrapChunks.push(
24672469
startScriptSrc,
24682470
stringToChunk(escapeTextForBrowser(src))
@@ -7467,6 +7469,42 @@ function preinit(href, options) {
74677469
}
74687470
}
74697471
}
7472+
} // This function is only safe to call at Request start time since it assumes
7473+
// that each script has not already been preloaded. If we find a need to preload
7474+
// scripts at any other point in time we will need to check whether the preload
7475+
// already exists and not assume it
7476+
7477+
function preloadBootstrapScript(resources, src, nonce, integrity) {
7478+
var key = getResourceKey("script", src);
7479+
7480+
{
7481+
if (resources.preloadsMap.has(key)) {
7482+
// This is coded as a React error because it should be impossible for a userspace preload to preempt this call
7483+
// If a userspace preload can preempt it then this assumption is broken and we need to reconsider this strategy
7484+
// rather than instruct the user to not preload their bootstrap scripts themselves
7485+
error(
7486+
'Internal React Error: React expected bootstrap script with src "%s" to not have been preloaded already. please file an issue',
7487+
src
7488+
);
7489+
}
7490+
}
7491+
7492+
var props = {
7493+
rel: "preload",
7494+
href: src,
7495+
as: "script",
7496+
nonce: nonce,
7497+
integrity: integrity
7498+
};
7499+
var resource = {
7500+
type: "preload",
7501+
chunks: [],
7502+
state: NoState,
7503+
props: props
7504+
};
7505+
resources.preloadsMap.set(key, resource);
7506+
resources.explicitScriptPreloads.add(resource);
7507+
pushLinkImpl(resource.chunks, props);
74707508
}
74717509

74727510
function internalPreinitScript(resources, src, chunks) {
@@ -7641,11 +7679,13 @@ function getAsResourceDEV(resource) {
76417679
}
76427680

76437681
function createResponseState(
7682+
resources,
76447683
generateStaticMarkup,
76457684
identifierPrefix,
76467685
externalRuntimeConfig
76477686
) {
76487687
var responseState = createResponseState$1(
7688+
resources,
76497689
identifierPrefix,
76507690
undefined,
76517691
undefined,
@@ -10072,6 +10112,7 @@ function noop() {}
1007210112

1007310113
function createRequest(
1007410114
children,
10115+
resources,
1007510116
responseState,
1007610117
rootFormatContext,
1007710118
progressiveChunkSize,
@@ -10084,7 +10125,6 @@ function createRequest(
1008410125
prepareHostDispatcher();
1008510126
var pingedTasks = [];
1008610127
var abortSet = new Set();
10087-
var resources = createResources();
1008810128
var request = {
1008910129
destination: null,
1009010130
flushScheduled: false,
@@ -12015,7 +12055,12 @@ function flushCompletedQueues(request, destination) {
1201512055
// We haven't flushed the root yet so we don't need to check any other branches further down
1201612056
return;
1201712057
}
12018-
} else if (enableFloat) {
12058+
} else if (request.pendingRootTasks > 0) {
12059+
// We have not yet flushed the root segment so we early return
12060+
return;
12061+
}
12062+
12063+
if (enableFloat) {
1201912064
writeHoistables(destination, request.resources, request.responseState);
1202012065
} // We emit client rendering instructions for already emitted boundaries first.
1202112066
// This is so that we can signal to the client to start client rendering them as
@@ -12233,9 +12278,12 @@ function renderToStringImpl(
1223312278
readyToStream = true;
1223412279
}
1223512280

12281+
var resources = createResources();
1223612282
var request = createRequest(
1223712283
children,
12284+
resources,
1223812285
createResponseState(
12286+
resources,
1223912287
generateStaticMarkup,
1224012288
options ? options.identifierPrefix : undefined,
1224112289
unstable_externalRuntimeSrc

0 commit comments

Comments
 (0)