Skip to content

Commit

Permalink
fix: take over prod tags
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Dec 14, 2023
1 parent 256c112 commit d339976
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions packages/client/src/setupUpdateHead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,12 @@ export const setupUpdateHead = (): void => {
}

let managedHeadElements: (HTMLElement | null)[] = []
let isFirstUpdate = true

const updateHeadTags = (newTags: HeadConfig[]): void => {
if (!__VUEPRESS_DEV__ && isFirstUpdate) {
// in production, the initial meta tags are already pre-rendered so we
// skip the first update.
isFirstUpdate = false
return
}

const updateHeadTags = (): void => {
document.documentElement.lang = lang.value

const newElements: (HTMLElement | null)[] = newTags.map(createHeadElement)
const newElements: (HTMLElement | null)[] =
head.value.map(createHeadElement)

managedHeadElements.forEach((oldEl, oldIndex) => {
const matchedIndex = newElements.findIndex(
Expand All @@ -57,18 +50,16 @@ export const setupUpdateHead = (): void => {
Boolean,
)
}
provide(updateHeadSymbol, () => {
updateHeadTags(head.value)
})
provide(updateHeadSymbol, updateHeadTags)

onMounted(() => {
watch(
() => head.value,
() => {
updateHeadTags(head.value)
},
{ immediate: true },
)
// in production, the initial meta tags are already pre-rendered,
// so we need to skip the first update and take over the existing tags.
if (!__VUEPRESS_DEV__) {
managedHeadElements = head.value.map(queryHeadElement).filter(Boolean)
}

watch(head, updateHeadTags, { immediate: __VUEPRESS_DEV__ })
})
}

Expand Down

0 comments on commit d339976

Please sign in to comment.