Skip to content

Commit d43dbd7

Browse files
authored
docs(landing): optimize fonts loading (#21075)
1 parent a532e68 commit d43dbd7

File tree

3 files changed

+46
-36
lines changed

3 files changed

+46
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*.log
88
/.vscode/
99
/docs/.vitepress/cache
10+
/docs/.vitepress/.temp
1011
/packages/vite/LICENSE
1112
dist
1213
dist-ssr

docs/.vitepress/config.ts

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
groupIconVitePlugin,
99
} from 'vitepress-plugin-group-icons'
1010
import llmstxt from 'vitepress-plugin-llms'
11-
import type { PluginOption } from 'vite'
1211
import { markdownItImageSize } from 'markdown-it-image-size'
1312
import packageJson from '../../packages/vite/package.json' with { type: 'json' }
1413
import { buildEnd } from './buildEnd.config'
@@ -95,30 +94,6 @@ export default defineConfig({
9594
'link',
9695
{ rel: 'alternate', type: 'application/rss+xml', href: '/blog.rss' },
9796
],
98-
['link', { rel: 'preconnect', href: 'https://fonts.googleapis.com' }],
99-
[
100-
'link',
101-
{
102-
rel: 'preconnect',
103-
href: 'https://fonts.gstatic.com',
104-
crossorigin: 'true',
105-
},
106-
],
107-
[
108-
'link',
109-
{
110-
rel: 'preload',
111-
href: 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=Manrope:wght@600&family=IBM+Plex+Mono:wght@400&display=swap',
112-
as: 'style',
113-
},
114-
],
115-
[
116-
'link',
117-
{
118-
rel: 'stylesheet',
119-
href: 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&family=Manrope:wght@600&family=IBM+Plex+Mono:wght@400&display=swap',
120-
},
121-
],
12297
inlineScript('banner.js'),
12398
['link', { rel: 'me', href: 'https://m.webtoo.ls/@vite' }],
12499
['meta', { property: 'og:type', content: 'website' }],
@@ -468,16 +443,34 @@ export default defineConfig({
468443
level: [2, 3],
469444
},
470445
},
471-
transformPageData(pageData) {
472-
const canonicalUrl = `${ogUrl}/${pageData.relativePath}`
473-
.replace(/\/index\.md$/, '/')
474-
.replace(/\.md$/, '')
475-
pageData.frontmatter.head ??= []
476-
pageData.frontmatter.head.unshift(
477-
['link', { rel: 'canonical', href: canonicalUrl }],
478-
['meta', { property: 'og:title', content: pageData.title }],
479-
)
480-
return pageData
446+
transformHead(ctx) {
447+
const path = ctx.page.replace(/(^|\/)index\.md$/, '$1').replace(/\.md$/, '')
448+
449+
if (path !== '404') {
450+
const canonicalUrl = path ? `${ogUrl}/${path}` : ogUrl
451+
ctx.head.push(
452+
['link', { rel: 'canonical', href: canonicalUrl }],
453+
['meta', { property: 'og:title', content: ctx.pageData.title }],
454+
)
455+
}
456+
457+
// For the landing page, move the google font links to the top for better performance
458+
if (path === '') {
459+
const googleFontLinks: HeadConfig[] = []
460+
for (let i = 0; i < ctx.head.length; i++) {
461+
const tag = ctx.head[i]
462+
if (
463+
tag[0] === 'link' &&
464+
(tag[1]?.href?.includes('fonts.googleapis.com') ||
465+
tag[1]?.href?.includes('fonts.gstatic.com'))
466+
) {
467+
ctx.head.splice(i, 1)
468+
googleFontLinks.push(tag)
469+
i--
470+
}
471+
}
472+
ctx.head.unshift(...googleFontLinks)
473+
}
481474
},
482475
markdown: {
483476
// languages used for twoslash and jsdocs in twoslash
@@ -520,7 +513,7 @@ Vite is a new breed of frontend build tooling that significantly improves the fr
520513
- A [build command](https://vite.dev/guide/build.md) that bundles your code with [Rollup](https://rollupjs.org), pre-configured to output highly optimized static assets for production.
521514
522515
In addition, Vite is highly extensible via its [Plugin API](https://vite.dev/guide/api-plugin.md) and [JavaScript API](https://vite.dev/guide/api-javascript.md) with full typing support.`,
523-
}) as PluginOption,
516+
}),
524517
],
525518
optimizeDeps: {
526519
include: [

docs/index.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ layout: home
99
aside: false
1010
editLink: false
1111
markdownStyles: false
12+
13+
head:
14+
- - link
15+
- rel: preconnect
16+
href: https://fonts.googleapis.com
17+
- - link
18+
- rel: preconnect
19+
href: https://fonts.gstatic.com
20+
crossorigin: ''
21+
- - link
22+
- rel: preload
23+
href: https://fonts.googleapis.com/css2?family=Manrope:wght@600&family=IBM+Plex+Mono:wght@400&display=swap
24+
as: style
25+
- - link
26+
- rel: stylesheet
27+
href: https://fonts.googleapis.com/css2?family=Manrope:wght@600&family=IBM+Plex+Mono:wght@400&display=swap
1228
---
1329

1430
<script setup>

0 commit comments

Comments
 (0)