@@ -82,6 +82,7 @@ const endInlineScript = stringToPrecomputedChunk('</script>');
8282
8383const startScriptSrc = stringToPrecomputedChunk ( '<script src="' ) ;
8484const startModuleSrc = stringToPrecomputedChunk ( '<script type="module" src="' ) ;
85+ const scriptIntegirty = stringToPrecomputedChunk ( '" integrity="' ) ;
8586const endAsyncScript = stringToPrecomputedChunk ( '" async=""></script>' ) ;
8687
8788/**
@@ -104,13 +105,17 @@ const scriptRegex = /(<\/|<)(s)(cript)/gi;
104105const scriptReplacer = ( match , prefix , s , suffix ) =>
105106 `${ prefix } ${ s === 's' ? '\\u0073' : '\\u0053' } ${ suffix } ` ;
106107
108+ export type BootstrapScriptDescriptor = {
109+ src : string ,
110+ integrity ?: string ,
111+ } ;
107112// Allows us to keep track of what we've already written so we can refer back to it.
108113export function createResponseState (
109114 identifierPrefix : string | void ,
110115 nonce : string | void ,
111116 bootstrapScriptContent : string | void ,
112- bootstrapScripts : Array < string > | void ,
113- bootstrapModules : Array < string > | void ,
117+ bootstrapScripts : Array < string | BootstrapScriptDescriptor > | void ,
118+ bootstrapModules : Array < string | BootstrapScriptDescriptor > | void ,
114119) : ResponseState {
115120 const idPrefix = identifierPrefix === undefined ? '' : identifierPrefix ;
116121 const inlineScriptWithNonce =
@@ -129,20 +134,42 @@ export function createResponseState(
129134 }
130135 if ( bootstrapScripts !== undefined ) {
131136 for ( let i = 0 ; i < bootstrapScripts . length ; i ++ ) {
137+ const scriptConfig = bootstrapScripts [ i ] ;
138+ const src =
139+ typeof scriptConfig === 'string' ? scriptConfig : scriptConfig . src ;
140+ const integrity =
141+ typeof scriptConfig === 'string' ? undefined : scriptConfig . integrity ;
132142 bootstrapChunks . push (
133143 startScriptSrc ,
134- stringToChunk ( escapeTextForBrowser ( bootstrapScripts [ i ] ) ) ,
135- endAsyncScript ,
144+ stringToChunk ( escapeTextForBrowser ( src ) ) ,
136145 ) ;
146+ if ( integrity ) {
147+ bootstrapChunks . push (
148+ scriptIntegirty ,
149+ stringToChunk ( escapeTextForBrowser ( integrity ) ) ,
150+ ) ;
151+ }
152+ bootstrapChunks . push ( endAsyncScript ) ;
137153 }
138154 }
139155 if ( bootstrapModules !== undefined ) {
140156 for ( let i = 0 ; i < bootstrapModules . length ; i ++ ) {
157+ const scriptConfig = bootstrapModules [ i ] ;
158+ const src =
159+ typeof scriptConfig === 'string' ? scriptConfig : scriptConfig . src ;
160+ const integrity =
161+ typeof scriptConfig === 'string' ? undefined : scriptConfig . integrity ;
141162 bootstrapChunks . push (
142163 startModuleSrc ,
143- stringToChunk ( escapeTextForBrowser ( bootstrapModules [ i ] ) ) ,
144- endAsyncScript ,
164+ stringToChunk ( escapeTextForBrowser ( src ) ) ,
145165 ) ;
166+ if ( integrity ) {
167+ bootstrapChunks . push (
168+ scriptIntegirty ,
169+ stringToChunk ( escapeTextForBrowser ( integrity ) ) ,
170+ ) ;
171+ }
172+ bootstrapChunks . push ( endAsyncScript ) ;
146173 }
147174 }
148175 return {
0 commit comments