Skip to content

Commit 01c0002

Browse files
committed
improve info exposed by getPreloadFiles()
1 parent 4d81fa8 commit 01c0002

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/server/template-renderer/index.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ export type ClientManifest = {
3030
}
3131
};
3232

33+
type PreloadFile = {
34+
file: string;
35+
extension: string;
36+
fileWithoutQuery: string;
37+
asType: string;
38+
};
39+
3340
export default class TemplateRenderer {
3441
options: TemplateRendererOptions;
3542
inject: boolean;
@@ -118,10 +125,19 @@ export default class TemplateRenderer {
118125
return this.renderPreloadLinks(context) + this.renderPrefetchLinks(context)
119126
}
120127

121-
getPreloadFiles (context: Object) {
128+
getPreloadFiles (context: Object): Array<PreloadFile> {
122129
const usedAsyncFiles = this.getUsedAsyncFiles(context)
123130
if (this.preloadFiles || usedAsyncFiles) {
124-
return (this.preloadFiles || []).concat(usedAsyncFiles || [])
131+
return (this.preloadFiles || []).concat(usedAsyncFiles || []).map(file => {
132+
const withoutQuery = file.replace(/\?.*/, '')
133+
const extension = path.extname(withoutQuery).slice(1)
134+
return {
135+
file,
136+
extension,
137+
fileWithoutQuery: withoutQuery,
138+
asType: getPreloadType(extension)
139+
}
140+
})
125141
} else {
126142
return []
127143
}
@@ -130,27 +146,24 @@ export default class TemplateRenderer {
130146
renderPreloadLinks (context: Object): string {
131147
const files = this.getPreloadFiles(context)
132148
if (files.length) {
133-
return files.map(file => {
149+
return files.map(({ file, extension, fileWithoutQuery, asType }) => {
134150
let extra = ''
135-
const withoutQuery = file.replace(/\?.*/, '')
136-
const ext = path.extname(withoutQuery).slice(1)
137-
const type = getPreloadType(ext)
138151
const shouldPreload = this.options.shouldPreload
139152
// by default, we only preload scripts or css
140-
if (!shouldPreload && type !== 'script' && type !== 'style') {
153+
if (!shouldPreload && asType !== 'script' && asType !== 'style') {
141154
return ''
142155
}
143156
// user wants to explicitly control what to preload
144-
if (shouldPreload && !shouldPreload(withoutQuery, type)) {
157+
if (shouldPreload && !shouldPreload(fileWithoutQuery, asType)) {
145158
return ''
146159
}
147-
if (type === 'font') {
148-
extra = ` type="font/${ext}" crossorigin`
160+
if (asType === 'font') {
161+
extra = ` type="font/${extension}" crossorigin`
149162
}
150163
return `<link rel="preload" href="${
151164
this.publicPath}/${file
152165
}"${
153-
type !== '' ? ` as="${type}"` : ''
166+
asType !== '' ? ` as="${asType}"` : ''
154167
}${
155168
extra
156169
}>`

0 commit comments

Comments
 (0)