Skip to content

Commit a065602

Browse files
committed
docs(landing): optimize fonts loading
1 parent ee10c26 commit a065602

File tree

3 files changed

+45
-35
lines changed

3 files changed

+45
-35
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.vitepress/cache
2+
.vitepress/.temp
23
dist
34
node_modules

.vitepress/config.ts

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
groupIconVitePlugin,
99
} from 'vitepress-plugin-group-icons'
1010

11-
1211
import { markdownItImageSize } from 'markdown-it-image-size'
1312
import packageJson from '../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' }],
@@ -475,16 +450,34 @@ export default defineConfig({
475450
level: [2, 3],
476451
},
477452
},
478-
transformPageData(pageData) {
479-
const canonicalUrl = `${ogUrl}/${pageData.relativePath}`
480-
.replace(/\/index\.md$/, '/')
481-
.replace(/\.md$/, '')
482-
pageData.frontmatter.head ??= []
483-
pageData.frontmatter.head.unshift(
484-
['link', { rel: 'canonical', href: canonicalUrl }],
485-
['meta', { property: 'og:title', content: pageData.title }],
486-
)
487-
return pageData
453+
transformHead(ctx) {
454+
const path = ctx.page.replace(/(^|\/)index\.md$/, '$1').replace(/\.md$/, '')
455+
456+
if (path !== '404') {
457+
const canonicalUrl = path ? `${ogUrl}/${path}` : ogUrl
458+
ctx.head.push(
459+
['link', { rel: 'canonical', href: canonicalUrl }],
460+
['meta', { property: 'og:title', content: ctx.pageData.title }],
461+
)
462+
}
463+
464+
// For the landing page, move the google font links to the top for better performance
465+
if (path === '') {
466+
const googleFontLinks: HeadConfig[] = []
467+
for (let i = 0; i < ctx.head.length; i++) {
468+
const tag = ctx.head[i]
469+
if (
470+
tag[0] === 'link' &&
471+
(tag[1]?.href?.includes('fonts.googleapis.com') ||
472+
tag[1]?.href?.includes('fonts.gstatic.com'))
473+
) {
474+
ctx.head.splice(i, 1)
475+
googleFontLinks.push(tag)
476+
i--
477+
}
478+
}
479+
ctx.head.unshift(...googleFontLinks)
480+
}
488481
},
489482
markdown: {
490483
// languages used for twoslash and jsdocs in twoslash

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)