-
Notifications
You must be signed in to change notification settings - Fork 906
Expand file tree
/
Copy pathrenderPagePreloadLinks.ts
More file actions
61 lines (53 loc) · 1.39 KB
/
renderPagePreloadLinks.ts
File metadata and controls
61 lines (53 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import type { App } from '@vuepress/core'
import type { OutputChunk } from 'rolldown'
/**
* Render preload links of current page
*/
export const renderPagePreloadLinks = ({
app,
outputEntryChunk,
pageChunkFiles,
}: {
app: App
outputEntryChunk: OutputChunk
pageChunkFiles: string[]
}): string => {
// shouldPreload option
const { shouldPreload } = app.options
// do not render preload links
if (shouldPreload === false) {
return ''
}
// dedupe entry chunks and page chunks
const preloadFiles = Array.from(
new Set([
outputEntryChunk.fileName,
...outputEntryChunk.imports,
...pageChunkFiles,
]),
)
return preloadFiles
.map((item) => {
// resolve file type
const type = item.endsWith('.js')
? 'script'
: item.endsWith('.css')
? 'style'
: ''
// by default, we only preload js and css
if (shouldPreload === true && type !== 'script' && type !== 'style') {
return ''
}
// user wants to explicitly control what to preload
if (shouldPreload !== true && !shouldPreload(item, type)) {
return ''
}
if (type === 'script') {
return `<link rel="modulepreload" href="${app.options.base}${item}">`
}
return `<link rel="preload" href="${app.options.base}${item}"${
type === '' ? '' : ` as="${type}"`
}>`
})
.join('')
}