@@ -55,7 +55,7 @@ export default class TemplateRenderer {
55
55
this . inject = options . inject !== false
56
56
// if no template option is provided, the renderer is created
57
57
// as a utility object for rendering assets like preload links and scripts.
58
-
58
+
59
59
const { template } = options
60
60
this . parsedTemplate = template
61
61
? typeof template === 'string'
@@ -133,7 +133,7 @@ export default class TemplateRenderer {
133
133
return (
134
134
// render links for css files
135
135
( cssFiles . length
136
- ? cssFiles . map ( ( { file } ) => `<link rel="stylesheet" href="${ this . publicPath } ${ file } ">` ) . join ( '' )
136
+ ? cssFiles . map ( ( { file } ) => `<link rel="stylesheet" href="${ this . publicPath } ${ file } "${ getNonceAttribute ( context ) } >` ) . join ( '' )
137
137
: '' ) +
138
138
// context.styles is a getter exposed by vue-style-loader which contains
139
139
// the inline component styles collected during SSR
@@ -177,7 +177,7 @@ export default class TemplateRenderer {
177
177
asType !== '' ? ` as="${ asType } "` : ''
178
178
} ${
179
179
extra
180
- } >`
180
+ } ${ getNonceAttribute ( context ) } >`
181
181
} ) . join ( '' )
182
182
} else {
183
183
return ''
@@ -198,7 +198,7 @@ export default class TemplateRenderer {
198
198
if ( alreadyRendered ( file ) ) {
199
199
return ''
200
200
}
201
- return `<link rel="prefetch" href="${ this . publicPath } ${ file } ">`
201
+ return `<link rel="prefetch" href="${ this . publicPath } ${ file } "${ getNonceAttribute ( context ) } >`
202
202
} ) . join ( '' )
203
203
} else {
204
204
return ''
@@ -214,9 +214,8 @@ export default class TemplateRenderer {
214
214
const autoRemove = process . env . NODE_ENV === 'production'
215
215
? ';(function(){var s;(s=document.currentScript||document.scripts[document.scripts.length-1]).parentNode.removeChild(s);}());'
216
216
: ''
217
- const nonceAttr = context . nonce ? ` nonce="${ context . nonce } "` : ''
218
217
return context [ contextKey ]
219
- ? `<script${ nonceAttr } >window.${ windowKey } =${ state } ${ autoRemove } </script>`
218
+ ? `<script${ getNonceAttribute ( context ) } >window.${ windowKey } =${ state } ${ autoRemove } </script>`
220
219
: ''
221
220
}
222
221
@@ -226,7 +225,7 @@ export default class TemplateRenderer {
226
225
const async = ( this . getUsedAsyncFiles ( context ) || [ ] ) . filter ( ( { file } ) => isJS ( file ) )
227
226
const needed = [ initial [ 0 ] ] . concat ( async , initial . slice ( 1 ) )
228
227
return needed . map ( ( { file } ) => {
229
- return `<script src="${ this . publicPath } ${ file } " defer></script>`
228
+ return `<script src="${ this . publicPath } ${ file } " defer${ getNonceAttribute ( context ) } ></script>`
230
229
} ) . join ( '' )
231
230
} else {
232
231
return ''
@@ -275,3 +274,7 @@ function getPreloadType (ext: string): string {
275
274
return ''
276
275
}
277
276
}
277
+
278
+ function getNonceAttribute ( context : Object ) : string {
279
+ return context . nonce ? ` nonce="${ context . nonce } "` : ''
280
+ }
0 commit comments