@@ -8,14 +8,24 @@ import {
8
8
import type { Route } from '../router'
9
9
10
10
export function useUpdateHead ( route : Route , siteDataByRouteRef : Ref < SiteData > ) {
11
- let managedHeadElements : ( HTMLElement | undefined ) [ ] = [ ]
12
11
let isFirstUpdate = true
12
+ let managedHeadElements : ( HTMLElement | undefined ) [ ] = [ ]
13
13
14
14
const updateHeadTags = ( newTags : HeadConfig [ ] ) => {
15
15
if ( import . meta. env . PROD && isFirstUpdate ) {
16
16
// in production, the initial meta tags are already pre-rendered so we
17
17
// skip the first update.
18
18
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
+ } )
19
29
return
20
30
}
21
31
@@ -96,3 +106,9 @@ function isMetaDescription(headConfig: HeadConfig) {
96
106
function filterOutHeadDescription ( head : HeadConfig [ ] ) {
97
107
return head . filter ( ( h ) => ! isMetaDescription ( h ) )
98
108
}
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