From 30539e6d8aa8ada22bfb41fd85e057b7229cadbd Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Sun, 17 Sep 2023 22:50:42 -0400 Subject: [PATCH] Attempt to convert resource maps to serializable sets --- .../src/server/ReactFizzConfigDOM.js | 169 ++++++++---------- 1 file changed, 79 insertions(+), 90 deletions(-) diff --git a/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js b/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js index d0990c96a6806..4677a98cdc052 100644 --- a/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js +++ b/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js @@ -152,6 +152,7 @@ export type RenderState = { fontPreloads: Set, highImagePreloads: Set, // usedImagePreloads: Set, + // TODO: Precedences needs to be seeded with the order of precedence after resuming. precedences: Map>, stylePrecedences: Map, bootstrapScripts: Set, @@ -186,10 +187,10 @@ export type ResumableState = { hasHtml: boolean, // Resources - Request local cache - preloadsMap: Map, - preconnectsMap: Map, - stylesMap: Map, - scriptsMap: Map, + preloadsMap: {[key: string]: null}, + preconnectsMap: {[key: string]: null}, + stylesMap: {[key: string]: null}, + scriptsMap: {[key: string]: null}, }; const dataElementQuotedEnd = stringToPrecomputedChunk('">'); @@ -491,10 +492,10 @@ export function createResumableState( // @TODO add bootstrap script to implicit preloads // persistent - preloadsMap: new Map(), - preconnectsMap: new Map(), - stylesMap: new Map(), - scriptsMap: new Map(), + preloadsMap: {}, + preconnectsMap: {}, + stylesMap: {}, + scriptsMap: {}, }; } @@ -2131,10 +2132,9 @@ function pushLink( return pushLinkImpl(target, props); } else { // This stylesheet refers to a Resource and we create a new one if necessary - let resource = resumableState.stylesMap.get(key); - if (!resource) { + if (!resumableState.stylesMap.hasOwnProperty(key)) { const resourceProps = stylesheetPropsFromRawProps(props); - const preloadResource = resumableState.preloadsMap.get(key); + const preloadResource = resumableState.preloadsMap.get(key); // TODO let state = NoState; if (preloadResource) { // If we already had a preload we don't want that resource to flush directly. @@ -2148,13 +2148,13 @@ function pushLink( state = PreloadFlushed; } } - resource = { + const resource = { type: 'stylesheet', chunks: ([]: Array), state, props: resourceProps, }; - resumableState.stylesMap.set(key, resource); + resumableState.stylesMap[key] = null; let precedenceSet = renderState.precedences.get(precedence); if (!precedenceSet) { precedenceSet = new Set(); @@ -2182,7 +2182,7 @@ function pushLink( precedenceSet.add(resource); } if (renderState.boundaryResources) { - renderState.boundaryResources.add(resource); + renderState.boundaryResources.add(resource); // TODO } if (textEmbedded) { // This link follows text but we aren't writing a tag. while not as efficient as possible we need @@ -2313,9 +2313,8 @@ function pushStyle( } const key = getResourceKey('style', href); - let resource = resumableState.stylesMap.get(key); - if (!resource) { - resource = renderState.stylePrecedences.get(precedence); + if (!resumableState.stylesMap.hasOwnProperty(key)) { + let resource = renderState.stylePrecedences.get(precedence); if (!resource) { resource = { type: 'style', @@ -2341,12 +2340,12 @@ function pushStyle( } else { resource.props.hrefs.push(href); } - resumableState.stylesMap.set(key, resource); - if (renderState.boundaryResources) { - renderState.boundaryResources.add(resource); - } + resumableState.stylesMap[key] = null; pushStyleContents(resource.chunks, props); } + if (renderState.boundaryResources) { + renderState.boundaryResources.add(resource); // TODO + } if (textEmbedded) { // This link follows text but we aren't writing a tag. while not as efficient as possible we need @@ -2499,9 +2498,8 @@ function pushImg( // resumableState. const {sizes} = props; const key = getImagePreloadKey(src, srcSet, sizes); - let resource = resumableState.preloadsMap.get(key); - if (!resource) { - resource = { + if (!resumableState.preloadsMap.hasOwnProperty(key)) { + const resource = { type: 'preload', chunks: [], state: NoState, @@ -2522,16 +2520,16 @@ function pushImg( referrerPolicy: props.referrerPolicy, }, }; - resumableState.preloadsMap.set(key, resource); + resumableState.preloadsMap[key] = resource; pushLinkImpl(resource.chunks, resource.props); - } - if ( - props.fetchPriority === 'high' || - renderState.highImagePreloads.size < 10 - ) { - renderState.highImagePreloads.add(resource); - } else { - renderState.bulkPreloads.add(resource); + if ( + props.fetchPriority === 'high' || + renderState.highImagePreloads.size < 10 + ) { + renderState.highImagePreloads.add(resource); + } else { + renderState.bulkPreloads.add(resource); + } } } return pushSelfClosing(target, props, 'img'); @@ -2867,20 +2865,19 @@ function pushScript( const src = props.src; const key = getResourceKey('script', src); // We can make this