Skip to content

Commit 24735db

Browse files
committed
perf(theme): preload font
1 parent 5dd2a23 commit 24735db

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/node/build/build.ts

+33-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { createRequire } from 'module'
1010
import { pathToFileURL } from 'url'
1111
import { packageDirectorySync } from 'pkg-dir'
1212
import { serializeFunctions } from '../utils/fnSerialize'
13+
import type { HeadConfig } from '../shared'
1314

1415
export async function build(
1516
root?: string,
@@ -65,6 +66,36 @@ export async function build(
6566
)
6667
.map((asset) => siteConfig.site.base + asset.fileName)
6768

69+
// default theme special handling: inject font preload
70+
// custom themes will need to use `transformHead` to inject this
71+
const additionalHeadTags: HeadConfig[] = []
72+
const isDefaultTheme =
73+
clientResult &&
74+
clientResult.output.some(
75+
(chunk) =>
76+
chunk.type === 'chunk' &&
77+
chunk.name === 'theme' &&
78+
chunk.moduleIds.some((id) => id.includes('client/theme-default'))
79+
)
80+
81+
if (isDefaultTheme) {
82+
const fontURL = assets.find((file) =>
83+
/inter-roman-latin\.\w+\.woff2/.test(file)
84+
)
85+
if (fontURL) {
86+
additionalHeadTags.push([
87+
'link',
88+
{
89+
rel: 'preload',
90+
href: fontURL,
91+
as: 'font',
92+
type: 'font/woff2',
93+
crossorigin: ''
94+
}
95+
])
96+
}
97+
}
98+
6899
// We embed the hash map and site config strings into each page directly
69100
// so that it doesn't alter the main chunk's hash on every build.
70101
// It's also embedded as a string and JSON.parsed from the client because
@@ -88,7 +119,8 @@ export async function build(
88119
assets,
89120
pageToHashMap,
90121
hashMapString,
91-
siteDataString
122+
siteDataString,
123+
additionalHeadTags
92124
)
93125
)
94126
)

src/node/build/render.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export async function renderPage(
2929
assets: string[],
3030
pageToHashMap: Record<string, string>,
3131
hashMapString: string,
32-
siteDataString: string
32+
siteDataString: string,
33+
additionalHeadTags: HeadConfig[]
3334
) {
3435
const routePath = `/${page.replace(/\.md$/, '')}`
3536
const siteData = resolveSiteDataByRoute(config.site, routePath)
@@ -64,6 +65,12 @@ export async function renderPage(
6465
}
6566
}
6667

68+
const title: string = createTitle(siteData, pageData)
69+
const description: string = pageData.description || siteData.description
70+
const stylesheetLink = cssChunk
71+
? `<link rel="preload stylesheet" href="${siteData.base}${cssChunk.fileName}" as="style">`
72+
: ''
73+
6774
let preloadLinks =
6875
config.mpa || (!hasCustom404 && page === '404.md')
6976
? []
@@ -100,14 +107,8 @@ export async function renderPage(
100107
const preloadHeadTags = toHeadTags(preloadLinks, 'modulepreload')
101108
const prefetchHeadTags = toHeadTags(prefetchLinks, 'prefetch')
102109

103-
const stylesheetLink = cssChunk
104-
? `<link rel="preload stylesheet" href="${siteData.base}${cssChunk.fileName}" as="style">`
105-
: ''
106-
107-
const title: string = createTitle(siteData, pageData)
108-
const description: string = pageData.description || siteData.description
109-
110110
const headBeforeTransform = [
111+
...additionalHeadTags,
111112
...preloadHeadTags,
112113
...prefetchHeadTags,
113114
...mergeHead(

0 commit comments

Comments
 (0)