@@ -150,7 +150,9 @@ export default class TemplateRenderer {
150
150
? cssFiles
151
151
. map (
152
152
( { file } ) =>
153
- `<link rel="stylesheet" href="${ this . publicPath } ${ file } ">`
153
+ `<link rel="stylesheet" href="${
154
+ this . publicPath
155
+ } ${ file } "${ getNonceAttribute ( context ) } >`
154
156
)
155
157
. join ( '' )
156
158
: '' ) +
@@ -193,7 +195,7 @@ export default class TemplateRenderer {
193
195
}
194
196
return `<link rel="preload" href="${ this . publicPath } ${ file } "${
195
197
asType !== '' ? ` as="${ asType } "` : ''
196
- } ${ extra } >`
198
+ } ${ extra } ${ getNonceAttribute ( context ) } >`
197
199
} )
198
200
. join ( '' )
199
201
} else {
@@ -216,7 +218,9 @@ export default class TemplateRenderer {
216
218
if ( alreadyRendered ( file ) ) {
217
219
return ''
218
220
}
219
- return `<link rel="prefetch" href="${ this . publicPath } ${ file } ">`
221
+ return `<link rel="prefetch" href="${
222
+ this . publicPath
223
+ } ${ file } "${ getNonceAttribute ( context ) } >`
220
224
} )
221
225
. join ( '' )
222
226
} else {
@@ -234,9 +238,10 @@ export default class TemplateRenderer {
234
238
const autoRemove = __DEV__
235
239
? ''
236
240
: ';(function(){var s;(s=document.currentScript||document.scripts[document.scripts.length-1]).parentNode.removeChild(s);}());'
237
- const nonceAttr = context . nonce ? ` nonce="${ context . nonce } "` : ''
238
241
return context [ contextKey ]
239
- ? `<script${ nonceAttr } >window.${ windowKey } =${ state } ${ autoRemove } </script>`
242
+ ? `<script${ getNonceAttribute (
243
+ context
244
+ ) } >window.${ windowKey } =${ state } ${ autoRemove } </script>`
240
245
: ''
241
246
}
242
247
@@ -249,7 +254,9 @@ export default class TemplateRenderer {
249
254
const needed = [ initial [ 0 ] ] . concat ( async , initial . slice ( 1 ) )
250
255
return needed
251
256
. map ( ( { file } ) => {
252
- return `<script src="${ this . publicPath } ${ file } " defer></script>`
257
+ return `<script src="${
258
+ this . publicPath
259
+ } ${ file } " defer${ getNonceAttribute ( context ) } ></script>`
253
260
} )
254
261
. join ( '' )
255
262
} else {
@@ -304,3 +311,7 @@ function getPreloadType(ext: string): string {
304
311
return ''
305
312
}
306
313
}
314
+
315
+ function getNonceAttribute ( context : Record < string , any > ) : string {
316
+ return context . nonce ? ` nonce="${ context . nonce } "` : ''
317
+ }
0 commit comments