-
Notifications
You must be signed in to change notification settings - Fork 906
Expand file tree
/
Copy pathrenderPagePrefetchLinks.ts
More file actions
38 lines (34 loc) · 998 Bytes
/
renderPagePrefetchLinks.ts
File metadata and controls
38 lines (34 loc) · 998 Bytes
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
import type { App } from '@vuepress/core'
import type { FileMeta } from './types.js'
/**
* Render prefetch links of current page
*/
export const renderPagePrefetchLinks = ({
app,
asyncFilesMeta,
pageClientFilesMeta,
}: {
app: App
asyncFilesMeta: FileMeta[]
pageClientFilesMeta: FileMeta[]
}): string => {
// shouldPrefetch option
const { shouldPrefetch } = app.options
// do not render prefetch links
if (shouldPrefetch === false) {
return ''
}
// async files excluding files used by current page should be prefetch
const prefetchFilesMeta = asyncFilesMeta.filter(
({ file }) => !pageClientFilesMeta.some((meta) => meta.file === file),
)
return prefetchFilesMeta
.map(({ file, type }) => {
// user wants to explicitly control what to prefetch
if (shouldPrefetch !== true && !shouldPrefetch(file, type)) {
return ''
}
return `<link rel="prefetch" href="${app.options.base}${file}" as="${type}">`
})
.join('')
}