Skip to content

Commit 5e2d853

Browse files
authored
fix(client): handle head orphans added in initial load (#3474)
1 parent 9510cd7 commit 5e2d853

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/client/app/composables/head.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@ import {
88
import type { Route } from '../router'
99

1010
export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
11-
let managedHeadElements: (HTMLElement | undefined)[] = []
1211
let isFirstUpdate = true
12+
let managedHeadElements: (HTMLElement | undefined)[] = []
1313

1414
const updateHeadTags = (newTags: HeadConfig[]) => {
1515
if (import.meta.env.PROD && isFirstUpdate) {
1616
// in production, the initial meta tags are already pre-rendered so we
1717
// skip the first update.
1818
isFirstUpdate = false
19+
newTags.forEach((tag) => {
20+
const selector = toSelector(tag[0], tag[1])
21+
const headEl = createHeadElement(tag)
22+
;[...document.querySelectorAll(selector)].some((el) => {
23+
if (el.isEqualNode(headEl)) {
24+
managedHeadElements.push(el as HTMLElement)
25+
return true
26+
}
27+
})
28+
})
1929
return
2030
}
2131

@@ -96,3 +106,9 @@ function isMetaDescription(headConfig: HeadConfig) {
96106
function filterOutHeadDescription(head: HeadConfig[]) {
97107
return head.filter((h) => !isMetaDescription(h))
98108
}
109+
110+
function toSelector(tag: string, attrs: Record<string, string>) {
111+
return `${tag}${Object.keys(attrs)
112+
.map((key) => `[${key}="${attrs[key].replace(/(["'\\])/g, '\\$1')}"]`)
113+
.join('')}`
114+
}

0 commit comments

Comments
 (0)