Skip to content

Commit e721d60

Browse files
committed
feat: shouldPreload hook
1 parent f5308d7 commit e721d60

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

src/node/build/render.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,43 @@ export async function renderPage(
3939
))
4040
const pageData = JSON.parse(__pageData)
4141

42-
const preloadLinks = (
43-
config.mpa
44-
? appChunk
45-
? [appChunk.fileName]
46-
: []
47-
: result && appChunk
48-
? [
49-
...new Set([
50-
// resolve imports for index.js + page.md.js and inject script tags for
51-
// them as well so we fetch everything as early as possible without having
52-
// to wait for entry chunks to parse
53-
...resolvePageImports(config, page, result, appChunk),
54-
pageClientJsFileName,
55-
appChunk.fileName
56-
])
57-
]
42+
let preloadLinks = config.mpa
43+
? appChunk
44+
? [appChunk.fileName]
5845
: []
59-
)
46+
: result && appChunk
47+
? [
48+
...new Set([
49+
// resolve imports for index.js + page.md.js and inject script tags for
50+
// them as well so we fetch everything as early as possible without having
51+
// to wait for entry chunks to parse
52+
...resolvePageImports(config, page, result, appChunk),
53+
pageClientJsFileName,
54+
appChunk.fileName
55+
])
56+
]
57+
: []
58+
59+
let prefetchLinks: string[] = []
60+
61+
const { shouldPreload } = config
62+
if (shouldPreload) {
63+
prefetchLinks = preloadLinks.filter((link) => !shouldPreload(link, page))
64+
preloadLinks = preloadLinks.filter((link) => shouldPreload(link, page))
65+
}
66+
67+
const preloadLinksString = preloadLinks
6068
.map((file) => {
6169
return `<link rel="modulepreload" href="${siteData.base}${file}">`
6270
})
6371
.join('\n ')
6472

73+
const prefetchLinkString = prefetchLinks
74+
.map((file) => {
75+
return `<link rel="prefetch" href="${siteData.base}${file}">`
76+
})
77+
.join('\n ')
78+
6579
const stylesheetLink = cssChunk
6680
? `<link rel="stylesheet" href="${siteData.base}${cssChunk.fileName}">`
6781
: ''
@@ -105,7 +119,8 @@ export async function renderPage(
105119
pageData.description || siteData.description
106120
}">
107121
${stylesheetLink}
108-
${preloadLinks}
122+
${preloadLinksString}
123+
${prefetchLinkString}
109124
${renderHead(head)}
110125
</head>
111126
<body>
@@ -135,7 +150,7 @@ function resolvePageImports(
135150
appChunk: OutputChunk
136151
) {
137152
// find the page's js chunk and inject script tags for its imports so that
138-
// they are start fetching as early as possible
153+
// they start fetching as early as possible
139154
const srcPath = normalizePath(
140155
fs.realpathSync(path.resolve(config.srcDir, page))
141156
)

src/node/config.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export interface UserConfig<ThemeConfig = any> {
4747

4848
srcDir?: string
4949
srcExclude?: string[]
50+
shouldPreload?: (link: string, page: string) => boolean
5051

5152
/**
5253
* Enable MPA / zero-JS mode
@@ -60,7 +61,11 @@ export type RawConfigExports =
6061
| Promise<UserConfig>
6162
| (() => UserConfig | Promise<UserConfig>)
6263

63-
export interface SiteConfig<ThemeConfig = any> {
64+
export interface SiteConfig<ThemeConfig = any>
65+
extends Pick<
66+
UserConfig,
67+
'markdown' | 'vue' | 'vite' | 'shouldPreload' | 'mpa'
68+
> {
6469
root: string
6570
srcDir: string
6671
site: SiteData<ThemeConfig>
@@ -70,10 +75,6 @@ export interface SiteConfig<ThemeConfig = any> {
7075
tempDir: string
7176
alias: AliasOptions
7277
pages: string[]
73-
markdown: MarkdownOptions | undefined
74-
vue: VuePluginOptions | undefined
75-
vite: ViteConfig | undefined
76-
mpa: boolean
7778
}
7879

7980
const resolve = (root: string, file: string) =>
@@ -127,6 +128,7 @@ export async function resolveConfig(
127128
alias: resolveAliases(themeDir),
128129
vue: userConfig.vue,
129130
vite: userConfig.vite,
131+
shouldPreload: userConfig.shouldPreload,
130132
mpa: !!userConfig.mpa
131133
}
132134

0 commit comments

Comments
 (0)