From 24735dbcde15be41bc2697a4ea44001a1a583511 Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 14 Mar 2023 16:43:19 +0800 Subject: [PATCH] perf(theme): preload font --- src/node/build/build.ts | 34 +++++++++++++++++++++++++++++++++- src/node/build/render.ts | 17 +++++++++-------- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/node/build/build.ts b/src/node/build/build.ts index 7f1dded8065f..dc27ec508349 100644 --- a/src/node/build/build.ts +++ b/src/node/build/build.ts @@ -10,6 +10,7 @@ import { createRequire } from 'module' import { pathToFileURL } from 'url' import { packageDirectorySync } from 'pkg-dir' import { serializeFunctions } from '../utils/fnSerialize' +import type { HeadConfig } from '../shared' export async function build( root?: string, @@ -65,6 +66,36 @@ export async function build( ) .map((asset) => siteConfig.site.base + asset.fileName) + // default theme special handling: inject font preload + // custom themes will need to use `transformHead` to inject this + const additionalHeadTags: HeadConfig[] = [] + const isDefaultTheme = + clientResult && + clientResult.output.some( + (chunk) => + chunk.type === 'chunk' && + chunk.name === 'theme' && + chunk.moduleIds.some((id) => id.includes('client/theme-default')) + ) + + if (isDefaultTheme) { + const fontURL = assets.find((file) => + /inter-roman-latin\.\w+\.woff2/.test(file) + ) + if (fontURL) { + additionalHeadTags.push([ + 'link', + { + rel: 'preload', + href: fontURL, + as: 'font', + type: 'font/woff2', + crossorigin: '' + } + ]) + } + } + // We embed the hash map and site config strings into each page directly // so that it doesn't alter the main chunk's hash on every build. // It's also embedded as a string and JSON.parsed from the client because @@ -88,7 +119,8 @@ export async function build( assets, pageToHashMap, hashMapString, - siteDataString + siteDataString, + additionalHeadTags ) ) ) diff --git a/src/node/build/render.ts b/src/node/build/render.ts index 0c04c5016918..4b63535e6023 100644 --- a/src/node/build/render.ts +++ b/src/node/build/render.ts @@ -29,7 +29,8 @@ export async function renderPage( assets: string[], pageToHashMap: Record, hashMapString: string, - siteDataString: string + siteDataString: string, + additionalHeadTags: HeadConfig[] ) { const routePath = `/${page.replace(/\.md$/, '')}` const siteData = resolveSiteDataByRoute(config.site, routePath) @@ -64,6 +65,12 @@ export async function renderPage( } } + const title: string = createTitle(siteData, pageData) + const description: string = pageData.description || siteData.description + const stylesheetLink = cssChunk + ? `` + : '' + let preloadLinks = config.mpa || (!hasCustom404 && page === '404.md') ? [] @@ -100,14 +107,8 @@ export async function renderPage( const preloadHeadTags = toHeadTags(preloadLinks, 'modulepreload') const prefetchHeadTags = toHeadTags(prefetchLinks, 'prefetch') - const stylesheetLink = cssChunk - ? `` - : '' - - const title: string = createTitle(siteData, pageData) - const description: string = pageData.description || siteData.description - const headBeforeTransform = [ + ...additionalHeadTags, ...preloadHeadTags, ...prefetchHeadTags, ...mergeHead(