@@ -82,6 +82,7 @@ const endInlineScript = stringToPrecomputedChunk('</script>');
82
82
83
83
const startScriptSrc = stringToPrecomputedChunk ( '<script src="' ) ;
84
84
const startModuleSrc = stringToPrecomputedChunk ( '<script type="module" src="' ) ;
85
+ const scriptIntegirty = stringToPrecomputedChunk ( '" integrity="' ) ;
85
86
const endAsyncScript = stringToPrecomputedChunk ( '" async=""></script>' ) ;
86
87
87
88
/**
@@ -104,13 +105,17 @@ const scriptRegex = /(<\/|<)(s)(cript)/gi;
104
105
const scriptReplacer = ( match , prefix , s , suffix ) =>
105
106
`${ prefix } ${ s === 's' ? '\\u0073' : '\\u0053' } ${ suffix } ` ;
106
107
108
+ export type BootstrapScriptDescriptor = {
109
+ src : string ,
110
+ integrity ?: string ,
111
+ } ;
107
112
// Allows us to keep track of what we've already written so we can refer back to it.
108
113
export function createResponseState (
109
114
identifierPrefix : string | void ,
110
115
nonce : string | void ,
111
116
bootstrapScriptContent : string | void ,
112
- bootstrapScripts : Array < string > | void ,
113
- bootstrapModules : Array < string > | void ,
117
+ bootstrapScripts : $ReadOnlyArray < string | BootstrapScriptDescriptor > | void ,
118
+ bootstrapModules : $ReadOnlyArray < string | BootstrapScriptDescriptor > | void ,
114
119
) : ResponseState {
115
120
const idPrefix = identifierPrefix === undefined ? '' : identifierPrefix ;
116
121
const inlineScriptWithNonce =
@@ -129,20 +134,42 @@ export function createResponseState(
129
134
}
130
135
if ( bootstrapScripts !== undefined ) {
131
136
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 ;
132
142
bootstrapChunks . push (
133
143
startScriptSrc ,
134
- stringToChunk ( escapeTextForBrowser ( bootstrapScripts [ i ] ) ) ,
135
- endAsyncScript ,
144
+ stringToChunk ( escapeTextForBrowser ( src ) ) ,
136
145
) ;
146
+ if ( integrity ) {
147
+ bootstrapChunks . push (
148
+ scriptIntegirty ,
149
+ stringToChunk ( escapeTextForBrowser ( integrity ) ) ,
150
+ ) ;
151
+ }
152
+ bootstrapChunks . push ( endAsyncScript ) ;
137
153
}
138
154
}
139
155
if ( bootstrapModules !== undefined ) {
140
156
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 ;
141
162
bootstrapChunks . push (
142
163
startModuleSrc ,
143
- stringToChunk ( escapeTextForBrowser ( bootstrapModules [ i ] ) ) ,
144
- endAsyncScript ,
164
+ stringToChunk ( escapeTextForBrowser ( src ) ) ,
145
165
) ;
166
+ if ( integrity ) {
167
+ bootstrapChunks . push (
168
+ scriptIntegirty ,
169
+ stringToChunk ( escapeTextForBrowser ( integrity ) ) ,
170
+ ) ;
171
+ }
172
+ bootstrapChunks . push ( endAsyncScript ) ;
146
173
}
147
174
}
148
175
return {
0 commit comments