Skip to content

Commit

Permalink
fix: avoid resetting head tags on hmr/page switch
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jun 15, 2021
1 parent 834fa76 commit f52f20e
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions src/client/app/composables/head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,51 @@ import { HeadConfig, SiteData } from '../../shared'
import { Route } from '../router'

export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
const metaTags: HTMLElement[] = Array.from(document.querySelectorAll('meta'))

let metaTagEls: HTMLElement[] = Array.from(document.querySelectorAll('meta'))
let isFirstUpdate = true

const updateHeadTags = (newTags: HeadConfig[]) => {
if (import.meta.env.PROD && isFirstUpdate) {
// in production, the initial meta tags are already pre-rendered so we
// skip the first update.
isFirstUpdate = false
return
}
metaTags.forEach((el) => document.head.removeChild(el))
metaTags.length = 0
if (newTags && newTags.length) {
newTags.forEach((headConfig) => {
const el = createHeadElement(headConfig)
document.head.appendChild(el)
metaTags.push(el)
})

const newEls: HTMLElement[] = []
const commonLength = Math.min(metaTagEls.length, newTags.length)
for (let i = 0; i < commonLength; i++) {
let el = metaTagEls[i]
const [tag, attrs] = newTags[i]
if (el.tagName.toLocaleLowerCase() === tag) {
for (const key in attrs) {
if (el.getAttribute(key) !== attrs[key]) {
el.setAttribute(key, attrs[key])
}
}
for (let i = 0; i < el.attributes.length; i++) {
const name = el.attributes[i].name
if (!(name in attrs)) {
el.removeAttribute(name)
}
}
} else {
document.head.removeChild(el)
el = createHeadElement(newTags[i])
document.head.append(el)
}
newEls.push(el)
}

metaTagEls
.slice(commonLength)
.forEach((el) => document.head.removeChild(el))
newTags.slice(commonLength).forEach((headConfig) => {
const el = createHeadElement(headConfig)
document.head.appendChild(el)
newEls.push(el)
})
metaTagEls = newEls
}

watchEffect(() => {
Expand Down

0 comments on commit f52f20e

Please sign in to comment.