feat: shouldPreload hook

pull/478/head
Evan You 3 years ago
parent f5308d746f
commit e721d60585

@ -39,8 +39,7 @@ export async function renderPage(
)) ))
const pageData = JSON.parse(__pageData) const pageData = JSON.parse(__pageData)
const preloadLinks = ( let preloadLinks = config.mpa
config.mpa
? appChunk ? appChunk
? [appChunk.fileName] ? [appChunk.fileName]
: [] : []
@ -56,12 +55,27 @@ export async function renderPage(
]) ])
] ]
: [] : []
)
let prefetchLinks: string[] = []
const { shouldPreload } = config
if (shouldPreload) {
prefetchLinks = preloadLinks.filter((link) => !shouldPreload(link, page))
preloadLinks = preloadLinks.filter((link) => shouldPreload(link, page))
}
const preloadLinksString = preloadLinks
.map((file) => { .map((file) => {
return `<link rel="modulepreload" href="${siteData.base}${file}">` return `<link rel="modulepreload" href="${siteData.base}${file}">`
}) })
.join('\n ') .join('\n ')
const prefetchLinkString = prefetchLinks
.map((file) => {
return `<link rel="prefetch" href="${siteData.base}${file}">`
})
.join('\n ')
const stylesheetLink = cssChunk const stylesheetLink = cssChunk
? `<link rel="stylesheet" href="${siteData.base}${cssChunk.fileName}">` ? `<link rel="stylesheet" href="${siteData.base}${cssChunk.fileName}">`
: '' : ''
@ -105,7 +119,8 @@ export async function renderPage(
pageData.description || siteData.description pageData.description || siteData.description
}"> }">
${stylesheetLink} ${stylesheetLink}
${preloadLinks} ${preloadLinksString}
${prefetchLinkString}
${renderHead(head)} ${renderHead(head)}
</head> </head>
<body> <body>
@ -135,7 +150,7 @@ function resolvePageImports(
appChunk: OutputChunk appChunk: OutputChunk
) { ) {
// find the page's js chunk and inject script tags for its imports so that // find the page's js chunk and inject script tags for its imports so that
// they are start fetching as early as possible // they start fetching as early as possible
const srcPath = normalizePath( const srcPath = normalizePath(
fs.realpathSync(path.resolve(config.srcDir, page)) fs.realpathSync(path.resolve(config.srcDir, page))
) )

@ -47,6 +47,7 @@ export interface UserConfig<ThemeConfig = any> {
srcDir?: string srcDir?: string
srcExclude?: string[] srcExclude?: string[]
shouldPreload?: (link: string, page: string) => boolean
/** /**
* Enable MPA / zero-JS mode * Enable MPA / zero-JS mode
@ -60,7 +61,11 @@ export type RawConfigExports =
| Promise<UserConfig> | Promise<UserConfig>
| (() => UserConfig | Promise<UserConfig>) | (() => UserConfig | Promise<UserConfig>)
export interface SiteConfig<ThemeConfig = any> { export interface SiteConfig<ThemeConfig = any>
extends Pick<
UserConfig,
'markdown' | 'vue' | 'vite' | 'shouldPreload' | 'mpa'
> {
root: string root: string
srcDir: string srcDir: string
site: SiteData<ThemeConfig> site: SiteData<ThemeConfig>
@ -70,10 +75,6 @@ export interface SiteConfig<ThemeConfig = any> {
tempDir: string tempDir: string
alias: AliasOptions alias: AliasOptions
pages: string[] pages: string[]
markdown: MarkdownOptions | undefined
vue: VuePluginOptions | undefined
vite: ViteConfig | undefined
mpa: boolean
} }
const resolve = (root: string, file: string) => const resolve = (root: string, file: string) =>
@ -127,6 +128,7 @@ export async function resolveConfig(
alias: resolveAliases(themeDir), alias: resolveAliases(themeDir),
vue: userConfig.vue, vue: userConfig.vue,
vite: userConfig.vite, vite: userConfig.vite,
shouldPreload: userConfig.shouldPreload,
mpa: !!userConfig.mpa mpa: !!userConfig.mpa
} }

Loading…
Cancel
Save