1
1
import { watchEffect , Ref } from 'vue'
2
- import { HeadConfig , SiteData , createTitle } from '../../shared'
2
+ import { HeadConfig , SiteData , createTitle , mergeHead } from '../../shared'
3
3
import { Route } from '../router'
4
4
5
5
export function useUpdateHead ( route : Route , siteDataByRouteRef : Ref < SiteData > ) {
@@ -14,50 +14,20 @@ export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
14
14
return
15
15
}
16
16
17
- const newEls : HTMLElement [ ] = [ ]
18
- const commonLength = Math . min ( managedHeadTags . length , newTags . length )
19
- for ( let i = 0 ; i < commonLength ; i ++ ) {
20
- let el = managedHeadTags [ i ]
21
- const [ tag , attrs , innerHTML = '' ] = newTags [ i ]
22
- if ( el . tagName . toLocaleLowerCase ( ) === tag ) {
23
- for ( const key in attrs ) {
24
- if ( el . getAttribute ( key ) !== attrs [ key ] ) {
25
- el . setAttribute ( key , attrs [ key ] )
26
- }
27
- }
28
- for ( let i = 0 ; i < el . attributes . length ; i ++ ) {
29
- const name = el . attributes [ i ] . name
30
- if ( ! ( name in attrs ) ) {
31
- el . removeAttribute ( name )
32
- }
33
- }
34
- if ( el . innerHTML !== innerHTML ) {
35
- el . innerHTML = innerHTML
36
- }
37
- } else {
38
- document . head . removeChild ( el )
39
- el = createHeadElement ( newTags [ i ] )
40
- document . head . append ( el )
41
- }
42
- newEls . push ( el )
43
- }
44
-
45
- managedHeadTags
46
- . slice ( commonLength )
47
- . forEach ( ( el ) => document . head . removeChild ( el ) )
48
- newTags . slice ( commonLength ) . forEach ( ( headConfig ) => {
17
+ managedHeadTags . forEach ( ( el ) => document . head . removeChild ( el ) )
18
+ managedHeadTags = [ ]
19
+ newTags . forEach ( ( headConfig ) => {
49
20
const el = createHeadElement ( headConfig )
50
21
document . head . appendChild ( el )
51
- newEls . push ( el )
22
+ managedHeadTags . push ( el )
52
23
} )
53
- managedHeadTags = newEls
54
24
}
55
25
56
26
watchEffect ( ( ) => {
57
27
const pageData = route . data
58
28
const siteData = siteDataByRouteRef . value
59
29
const pageDescription = pageData && pageData . description
60
- const frontmatterHead = pageData && pageData . frontmatter . head
30
+ const frontmatterHead = ( pageData && pageData . frontmatter . head ) || [ ]
61
31
62
32
// update title and description
63
33
document . title = createTitle ( siteData , pageData )
@@ -66,11 +36,9 @@ export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
66
36
. querySelector ( `meta[name=description]` ) !
67
37
. setAttribute ( 'content' , pageDescription || siteData . description )
68
38
69
- updateHeadTags ( [
70
- // site head can only change during dev
71
- ...( import . meta. env . DEV ? siteData . head : [ ] ) ,
72
- ...( frontmatterHead ? filterOutHeadDescription ( frontmatterHead ) : [ ] )
73
- ] )
39
+ updateHeadTags (
40
+ mergeHead ( siteData . head , filterOutHeadDescription ( frontmatterHead ) )
41
+ )
74
42
} )
75
43
}
76
44
0 commit comments