@@ -153,6 +153,8 @@ export type BootstrapScriptDescriptor = {
153153 integrity ?: string ,
154154} ;
155155// Allows us to keep track of what we've already written so we can refer back to it.
156+ // if passed externalRuntimeConfig and the enableFizzExternalRuntime feature flag
157+ // is set, the server will send instructions via data attributes (instead of inline scripts)
156158export function createResponseState (
157159 identifierPrefix : string | void ,
158160 nonce : string | void ,
@@ -2291,7 +2293,7 @@ const completeSegmentScript1Full = stringToPrecomputedChunk(
22912293) ;
22922294const completeSegmentScript1Partial = stringToPrecomputedChunk ( '$RS("' ) ;
22932295const completeSegmentScript2 = stringToPrecomputedChunk ( '","' ) ;
2294- const completeSegmentScript3 = stringToPrecomputedChunk ( '")</script>' ) ;
2296+ const completeSegmentScriptEnd = stringToPrecomputedChunk ( '")</script>' ) ;
22952297
22962298export function writeCompletedSegmentInstruction (
22972299 destination : Destination ,
@@ -2307,13 +2309,16 @@ export function writeCompletedSegmentInstruction(
23072309 // Future calls can just reuse the same function.
23082310 writeChunk ( destination , completeSegmentScript1Partial ) ;
23092311 }
2312+
2313+ // Write function arguments, which are string literals
23102314 writeChunk ( destination , responseState . segmentPrefix ) ;
23112315 const formattedID = stringToChunk ( contentSegmentID . toString ( 16 ) ) ;
23122316 writeChunk ( destination , formattedID ) ;
23132317 writeChunk ( destination , completeSegmentScript2 ) ;
23142318 writeChunk ( destination , responseState . placeholderPrefix ) ;
23152319 writeChunk ( destination , formattedID ) ;
2316- return writeChunkAndReturn ( destination , completeSegmentScript3 ) ;
2320+
2321+ return writeChunkAndReturn ( destination , completeSegmentScriptEnd ) ;
23172322}
23182323
23192324const completeBoundaryScript1Full = stringToPrecomputedChunk (
@@ -2331,9 +2336,9 @@ const completeBoundaryWithStylesScript1Partial = stringToPrecomputedChunk(
23312336 '$RR("' ,
23322337) ;
23332338const completeBoundaryScript2 = stringToPrecomputedChunk ( '","' ) ;
2334- const completeBoundaryScript2a = stringToPrecomputedChunk ( '",' ) ;
2335- const completeBoundaryScript3 = stringToPrecomputedChunk ( '"' ) ;
2336- const completeBoundaryScript4 = stringToPrecomputedChunk ( ')</script>' ) ;
2339+ const completeBoundaryScript3a = stringToPrecomputedChunk ( '",' ) ;
2340+ const completeBoundaryScript3b = stringToPrecomputedChunk ( '"' ) ;
2341+ const completeBoundaryScriptEnd = stringToPrecomputedChunk ( ')</script>' ) ;
23372342
23382343export function writeCompletedBoundaryInstruction (
23392344 destination : Destination ,
@@ -2373,26 +2378,28 @@ export function writeCompletedBoundaryInstruction(
23732378 ) ;
23742379 }
23752380
2381+ // Write function arguments, which are string literals and array
23762382 const formattedContentID = stringToChunk ( contentSegmentID . toString ( 16 ) ) ;
23772383 writeChunk ( destination , boundaryID ) ;
23782384 writeChunk ( destination , completeBoundaryScript2 ) ;
23792385 writeChunk ( destination , responseState . segmentPrefix ) ;
23802386 writeChunk ( destination , formattedContentID ) ;
23812387 if ( enableFloat && hasStyleDependencies ) {
2382- writeChunk ( destination , completeBoundaryScript2a ) ;
2388+ writeChunk ( destination , completeBoundaryScript3a ) ;
2389+ // boundaryResources encodes an array literal
23832390 writeStyleResourceDependencies ( destination , boundaryResources ) ;
23842391 } else {
2385- writeChunk ( destination , completeBoundaryScript3 ) ;
2392+ writeChunk ( destination , completeBoundaryScript3b ) ;
23862393 }
2387- return writeChunkAndReturn ( destination , completeBoundaryScript4 ) ;
2394+ return writeChunkAndReturn ( destination , completeBoundaryScriptEnd ) ;
23882395}
23892396
23902397const clientRenderScript1Full = stringToPrecomputedChunk (
23912398 clientRenderFunction + ';$RX("' ,
23922399) ;
23932400const clientRenderScript1Partial = stringToPrecomputedChunk ( '$RX("' ) ;
23942401const clientRenderScript1A = stringToPrecomputedChunk ( '"' ) ;
2395- const clientRenderScript2 = stringToPrecomputedChunk ( ')</script>' ) ;
2402+ const clientRenderScriptEnd = stringToPrecomputedChunk ( ')</script>' ) ;
23962403const clientRenderErrorScriptArgInterstitial = stringToPrecomputedChunk ( ',' ) ;
23972404
23982405export function writeClientRenderBoundaryInstruction (
@@ -2418,31 +2425,34 @@ export function writeClientRenderBoundaryInstruction(
24182425 'An ID must have been assigned before we can complete the boundary.' ,
24192426 ) ;
24202427 }
2421-
2428+ // Write function arguments, which are string literals
24222429 writeChunk ( destination , boundaryID ) ;
24232430 writeChunk ( destination , clientRenderScript1A ) ;
24242431 if ( errorDigest || errorMessage || errorComponentStack ) {
2432+ // ,"JSONString"
24252433 writeChunk ( destination , clientRenderErrorScriptArgInterstitial ) ;
24262434 writeChunk (
24272435 destination ,
24282436 stringToChunk ( escapeJSStringsForInstructionScripts ( errorDigest || '' ) ) ,
24292437 ) ;
24302438 }
24312439 if ( errorMessage || errorComponentStack ) {
2440+ // ,"JSONString"
24322441 writeChunk ( destination , clientRenderErrorScriptArgInterstitial ) ;
24332442 writeChunk (
24342443 destination ,
24352444 stringToChunk ( escapeJSStringsForInstructionScripts ( errorMessage || '' ) ) ,
24362445 ) ;
24372446 }
24382447 if ( errorComponentStack ) {
2448+ // ,"JSONString"
24392449 writeChunk ( destination , clientRenderErrorScriptArgInterstitial ) ;
24402450 writeChunk (
24412451 destination ,
24422452 stringToChunk ( escapeJSStringsForInstructionScripts ( errorComponentStack ) ) ,
24432453 ) ;
24442454 }
2445- return writeChunkAndReturn ( destination , clientRenderScript2 ) ;
2455+ return writeChunkAndReturn ( destination , clientRenderScriptEnd ) ;
24462456}
24472457
24482458const regexForJSStringsInInstructionScripts = / [ < \u2028\u2029 ] / g ;
@@ -2743,6 +2753,16 @@ const arraySubsequentOpenBracket = stringToPrecomputedChunk(',[');
27432753const arrayInterstitial = stringToPrecomputedChunk ( ',' ) ;
27442754const arrayCloseBracket = stringToPrecomputedChunk ( ']' ) ;
27452755
2756+ function writeStyleResourceObject ( destination : Destination , str : string ) {
2757+ // write "script_escaped_string", since this is writing to a script tag
2758+ // and will be evaluated as a string literal inside an array literal
2759+ writeChunk (
2760+ destination ,
2761+ stringToChunk ( escapeJSObjectForInstructionScripts ( str ) ) ,
2762+ ) ;
2763+ // TODO(mofeiZ): will add a data writer format here in a later PR
2764+ }
2765+
27462766function writeStyleResourceDependencies (
27472767 destination : Destination ,
27482768 boundaryResources : BoundaryResources ,
@@ -2787,10 +2807,7 @@ function writeStyleResourceDependencyHrefOnly(
27872807 checkAttributeStringCoercion ( href , 'href' ) ;
27882808 }
27892809 const coercedHref = '' + ( href : any ) ;
2790- writeChunk (
2791- destination ,
2792- stringToChunk ( escapeJSObjectForInstructionScripts ( coercedHref ) ) ,
2793- ) ;
2810+ writeStyleResourceObject ( destination , coercedHref ) ;
27942811}
27952812
27962813function writeStyleResourceDependency (
@@ -2804,20 +2821,15 @@ function writeStyleResourceDependency(
28042821 }
28052822 const coercedHref = '' + ( href : any ) ;
28062823 sanitizeURL ( coercedHref ) ;
2807- writeChunk (
2808- destination ,
2809- stringToChunk ( escapeJSObjectForInstructionScripts ( coercedHref ) ) ,
2810- ) ;
2824+ writeStyleResourceObject ( destination , coercedHref ) ;
28112825
28122826 if ( __DEV__ ) {
28132827 checkAttributeStringCoercion ( precedence , 'precedence' ) ;
28142828 }
28152829 const coercedPrecedence = '' + ( precedence : any ) ;
28162830 writeChunk ( destination , arrayInterstitial ) ;
2817- writeChunk (
2818- destination ,
2819- stringToChunk ( escapeJSObjectForInstructionScripts ( coercedPrecedence ) ) ,
2820- ) ;
2831+
2832+ writeStyleResourceObject ( destination , coercedPrecedence ) ;
28212833
28222834 for ( const propKey in props ) {
28232835 if ( hasOwnProperty . call ( props , propKey ) ) {
@@ -2916,13 +2928,7 @@ function writeStyleResourceAttribute(
29162928 }
29172929 attributeValue = '' + ( value : any ) ;
29182930 writeChunk ( destination , arrayInterstitial ) ;
2919- writeChunk (
2920- destination ,
2921- stringToChunk ( escapeJSObjectForInstructionScripts ( attributeName ) ) ,
2922- ) ;
2931+ writeStyleResourceObject ( destination , attributeName ) ;
29232932 writeChunk ( destination , arrayInterstitial ) ;
2924- writeChunk (
2925- destination ,
2926- stringToChunk ( escapeJSObjectForInstructionScripts ( attributeValue ) ) ,
2927- ) ;
2933+ writeStyleResourceObject ( destination , attributeValue ) ;
29282934}
0 commit comments