Skip to content

Commit 60f646e

Browse files
committed
Track PreloadsProps on the resumable map
We have to ensure these are serializable. We transfer them to the resume so that they can be applied to resources discovered during the resume.
1 parent bdb495c commit 60f646e

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export type ResumableState = {
189189
hasHtml: boolean,
190190

191191
// Resources - Request local cache
192-
preloadsMap: {[key: string]: null},
192+
preloadsMap: {[key: string]: PreloadProps},
193193
preconnectsMap: {[key: string]: null},
194194
stylesMap: {[key: string]: null},
195195
scriptsMap: {[key: string]: null},
@@ -2139,23 +2139,23 @@ function pushLink(
21392139
let stylesInPrecedence = renderState.precedences.get(precedence);
21402140
if (!resumableState.stylesMap.hasOwnProperty(key)) {
21412141
const resourceProps = stylesheetPropsFromRawProps(props);
2142-
const preloadResource = renderState.preloadsMap.get(key);
21432142
let state = NoState;
2144-
if (preloadResource) {
2145-
// If we already had a preload we don't want that resource to flush directly.
2146-
// We let the newly created resource govern flushing.
2147-
preloadResource.state |= Blocked;
2148-
adoptPreloadPropsForStylesheetProps(
2149-
resourceProps,
2150-
preloadResource.props,
2151-
);
2152-
if (preloadResource.state & Flushed) {
2143+
if (resumableState.preloadsMap.hasOwnProperty(key)) {
2144+
const preloadProps: PreloadProps = resumableState.preloadsMap[key];
2145+
adoptPreloadPropsForStylesheetProps(resourceProps, preloadProps);
2146+
const preloadResource = renderState.preloadsMap.get(key);
2147+
if (preloadResource) {
2148+
// If we already had a preload we don't want that resource to flush directly.
2149+
// We let the newly created resource govern flushing.
2150+
preloadResource.state |= Blocked;
2151+
if (preloadResource.state & Flushed) {
2152+
state = PreloadFlushed;
2153+
}
2154+
} else {
2155+
// If we resumed then we assume that this was already flushed
2156+
// by the shell.
21532157
state = PreloadFlushed;
21542158
}
2155-
} else if (resumableState.preloadsMap.hasOwnProperty(key)) {
2156-
// If we resumed then we assume that this was already flushed
2157-
// by the shell.
2158-
state = PreloadFlushed;
21592159
}
21602160
const resource = {
21612161
type: 'stylesheet',
@@ -2549,7 +2549,7 @@ function pushImg(
25492549
referrerPolicy: props.referrerPolicy,
25502550
},
25512551
};
2552-
resumableState.preloadsMap[key] = null;
2552+
resumableState.preloadsMap[key] = resource.props;
25532553
renderState.preloadsMap.set(key, resource);
25542554
pushLinkImpl(resource.chunks, resource.props);
25552555
if (
@@ -2907,13 +2907,16 @@ function pushScript(
29072907
renderState.scripts.add(resource);
29082908

29092909
let scriptProps = props;
2910-
const preloadResource = renderState.preloadsMap.get(key);
2911-
if (preloadResource) {
2912-
// If we already had a preload we don't want that resource to flush directly.
2913-
// We let the newly created resource govern flushing.
2914-
preloadResource.state |= Blocked;
2910+
if (resumableState.preloadsMap.hasOwnProperty(key)) {
2911+
const preloadProps: PreloadProps = resumableState.preloadsMap[key];
29152912
scriptProps = {...props};
2916-
adoptPreloadPropsForScriptProps(scriptProps, preloadResource.props);
2913+
adoptPreloadPropsForScriptProps(scriptProps, preloadProps);
2914+
const preloadResource = renderState.preloadsMap.get(key);
2915+
if (preloadResource) {
2916+
// If we already had a preload we don't want that resource to flush directly.
2917+
// We let the newly created resource govern flushing.
2918+
preloadResource.state |= Blocked;
2919+
}
29172920
}
29182921
// encode the tag as Chunks
29192922
pushScriptImpl(resource.chunks, scriptProps);
@@ -4958,12 +4961,12 @@ type PreloadAsProps = {
49584961
rel: 'preload',
49594962
as: string,
49604963
href: ?string,
4961-
[string]: mixed,
4964+
[string]: ?string,
49624965
};
49634966
type PreloadModuleProps = {
49644967
rel: 'modulepreload',
49654968
href: ?string,
4966-
[string]: mixed,
4969+
[string]: ?string,
49674970
};
49684971
type PreloadProps = PreloadAsProps | PreloadModuleProps;
49694972
type PreloadResource = TResource<'preload', PreloadProps>;
@@ -5135,9 +5138,9 @@ function preload(href: string, as: string, options?: ?PreloadImplOptions) {
51355138
state: NoState,
51365139
props,
51375140
};
5138-
resumableState.preloadsMap[key] = null;
5141+
resumableState.preloadsMap[key] = props;
51395142
renderState.preloadsMap.set(key, resource);
5140-
pushLinkImpl(resource.chunks, resource.props);
5143+
pushLinkImpl(resource.chunks, props);
51415144
if (as === 'font') {
51425145
renderState.fontPreloads.add(resource);
51435146
} else if (as === 'image' && resource.props.fetchPriority === 'high') {
@@ -5186,7 +5189,7 @@ function preloadModule(
51865189
state: NoState,
51875190
props,
51885191
};
5189-
resumableState.preloadsMap[key] = null;
5192+
resumableState.preloadsMap[key] = props;
51905193
renderState.preloadsMap.set(key, resource);
51915194
pushLinkImpl(resource.chunks, resource.props);
51925195
renderState.bulkPreloads.add(resource);
@@ -5397,7 +5400,7 @@ function preloadBootstrapScript(
53975400
state: NoState,
53985401
props,
53995402
};
5400-
resumableState.preloadsMap[key] = null;
5403+
resumableState.preloadsMap[key] = props;
54015404
renderState.preloadsMap.set(key, resource);
54025405
renderState.bootstrapScripts.add(resource);
54035406
pushLinkImpl(resource.chunks, props);
@@ -5441,7 +5444,7 @@ function preloadBootstrapModule(
54415444
state: NoState,
54425445
props,
54435446
};
5444-
resumableState.preloadsMap[key] = null;
5447+
resumableState.preloadsMap[key] = props;
54455448
renderState.preloadsMap.set(key, resource);
54465449
renderState.bootstrapScripts.add(resource);
54475450
pushLinkImpl(resource.chunks, props);

0 commit comments

Comments
 (0)