Skip to content

Commit 6e45bfe

Browse files
committed
feat(ssr): support coustom attributes in render scripts
1 parent 841bb08 commit 6e45bfe

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

src/server/template-renderer/index.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default class TemplateRenderer {
5555
this.inject = options.inject !== false
5656
// if no template option is provided, the renderer is created
5757
// as a utility object for rendering assets like preload links and scripts.
58-
58+
5959
const { template } = options
6060
this.parsedTemplate = template
6161
? typeof template === 'string'
@@ -156,10 +156,14 @@ export default class TemplateRenderer {
156156

157157
renderPreloadLinks (context: Object): string {
158158
const files = this.getPreloadFiles(context)
159-
const shouldPreload = this.options.shouldPreload
160159
if (files.length) {
161-
return files.map(({ file, extension, fileWithoutQuery, asType }) => {
162-
let extra = ''
160+
const { getPreloadLinkAttrs, shouldPreload } = this.options
161+
const hasAttrsFn = typeof getPreloadLinkAttrs === 'function'
162+
return files.map(ref => {
163+
if(hasAttrsFn) {
164+
ref = getPreloadLinkAttrs(ref)
165+
}
166+
const { file, extension, fileWithoutQuery, asType, attrs } = ref
163167
// by default, we only preload scripts or css
164168
if (!shouldPreload && asType !== 'script' && asType !== 'style') {
165169
return ''
@@ -168,6 +172,9 @@ export default class TemplateRenderer {
168172
if (shouldPreload && !shouldPreload(fileWithoutQuery, asType)) {
169173
return ''
170174
}
175+
176+
let extra = attrs || ''
177+
171178
if (asType === 'font') {
172179
extra = ` type="font/${extension}" crossorigin`
173180
}
@@ -185,20 +192,26 @@ export default class TemplateRenderer {
185192
}
186193

187194
renderPrefetchLinks (context: Object): string {
188-
const shouldPrefetch = this.options.shouldPrefetch
195+
const { getPrefetchLinkAttrs, shouldPrefetch } = this.options
189196
if (this.prefetchFiles) {
190197
const usedAsyncFiles = this.getUsedAsyncFiles(context)
191198
const alreadyRendered = file => {
192199
return usedAsyncFiles && usedAsyncFiles.some(f => f.file === file)
193200
}
194-
return this.prefetchFiles.map(({ file, fileWithoutQuery, asType }) => {
201+
const hasAttrsFn = typeof getPrefetchLinkAttrs === 'function'
202+
return this.prefetchFiles.map(ref => {
203+
if(hasAttrsFn) {
204+
ref = getPrefetchLinkAttrs(ref)
205+
}
206+
const { file, fileWithoutQuery, asType } = ref
195207
if (shouldPrefetch && !shouldPrefetch(fileWithoutQuery, asType)) {
196208
return ''
197209
}
198210
if (alreadyRendered(file)) {
199211
return ''
200212
}
201-
return `<link rel="prefetch" href="${this.publicPath}${file}">`
213+
const { attrs='rel="prefetch"' } = ref
214+
return `<link ${attrs} href="${this.publicPath}${file}">`
202215
}).join('')
203216
} else {
204217
return ''
@@ -225,8 +238,14 @@ export default class TemplateRenderer {
225238
const initial = this.preloadFiles.filter(({ file }) => isJS(file))
226239
const async = (this.getUsedAsyncFiles(context) || []).filter(({ file }) => isJS(file))
227240
const needed = [initial[0]].concat(async, initial.slice(1))
228-
return needed.map(({ file }) => {
229-
return `<script src="${this.publicPath}${file}" defer></script>`
241+
const { getScriptAttrs } = this.options
242+
const hasAttrsFn = typeof getScriptAttrs === 'function'
243+
return needed.map((ref) => {
244+
if(hasAttrsFn) {
245+
ref = getScriptAttrs(ref)
246+
}
247+
const { file, attrs = 'defer' } = ref
248+
return `<script src="${this.publicPath}${file}" ${attrs}></script>`
230249
}).join('')
231250
} else {
232251
return ''

0 commit comments

Comments
 (0)