Skip to content

Commit e6bb5a4

Browse files
committed
perf: only update necessary head tags in prod
1 parent c046905 commit e6bb5a4

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/client/app/composables/head.ts

+21-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { HeadConfig, SiteData } from '../../shared'
33
import { Route } from '../router'
44

55
export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
6-
let metaTagEls: HTMLElement[] = Array.from(document.querySelectorAll('meta'))
6+
let managedHeadTags: HTMLElement[] = []
77
let isFirstUpdate = true
88

99
const updateHeadTags = (newTags: HeadConfig[]) => {
@@ -14,11 +14,14 @@ export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
1414
return
1515
}
1616

17+
console.log(managedHeadTags)
18+
console.log(newTags)
19+
1720
const newEls: HTMLElement[] = []
18-
const commonLength = Math.min(metaTagEls.length, newTags.length)
21+
const commonLength = Math.min(managedHeadTags.length, newTags.length)
1922
for (let i = 0; i < commonLength; i++) {
20-
let el = metaTagEls[i]
21-
const [tag, attrs] = newTags[i]
23+
let el = managedHeadTags[i]
24+
const [tag, attrs, innerHTML = ''] = newTags[i]
2225
if (el.tagName.toLocaleLowerCase() === tag) {
2326
for (const key in attrs) {
2427
if (el.getAttribute(key) !== attrs[key]) {
@@ -31,6 +34,9 @@ export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
3134
el.removeAttribute(name)
3235
}
3336
}
37+
if (el.innerHTML !== innerHTML) {
38+
el.innerHTML = innerHTML
39+
}
3440
} else {
3541
document.head.removeChild(el)
3642
el = createHeadElement(newTags[i])
@@ -39,15 +45,15 @@ export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
3945
newEls.push(el)
4046
}
4147

42-
metaTagEls
48+
managedHeadTags
4349
.slice(commonLength)
4450
.forEach((el) => document.head.removeChild(el))
4551
newTags.slice(commonLength).forEach((headConfig) => {
4652
const el = createHeadElement(headConfig)
4753
document.head.appendChild(el)
4854
newEls.push(el)
4955
})
50-
metaTagEls = newEls
56+
managedHeadTags = newEls
5157
}
5258

5359
watchEffect(() => {
@@ -56,25 +62,17 @@ export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
5662
const pageTitle = pageData && pageData.title
5763
const pageDescription = pageData && pageData.description
5864
const frontmatterHead = pageData && pageData.frontmatter.head
65+
66+
// update title and description
5967
document.title = (pageTitle ? pageTitle + ` | ` : ``) + siteData.title
68+
document
69+
.querySelector(`meta[name=description]`)!
70+
.setAttribute('content', pageDescription || siteData.description)
71+
6072
updateHeadTags([
61-
['meta', { charset: 'utf-8' }],
62-
[
63-
'meta',
64-
{
65-
name: 'viewport',
66-
content: 'width=device-width,initial-scale=1'
67-
}
68-
],
69-
[
70-
'meta',
71-
{
72-
name: 'description',
73-
content: pageDescription || siteData.description
74-
}
75-
],
76-
...siteData.head,
77-
...((frontmatterHead && filterOutHeadDescription(frontmatterHead)) || [])
73+
// site head can only change during dev
74+
...(import.meta.env.DEV ? siteData.head : []),
75+
...(frontmatterHead ? filterOutHeadDescription(frontmatterHead) : [])
7876
])
7977
})
8078
}

src/node/plugin.ts

+3
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ export function createVitePressPlugin(
124124
<!DOCTYPE html>
125125
<html>
126126
<head>
127+
<title></title>
127128
<meta charset="utf-8">
129+
<meta name="viewport" content="width=device-width,initial-scale=1">
130+
<meta name="description" content="">
128131
</head>
129132
<body>
130133
<div id="app"></div>

0 commit comments

Comments
 (0)