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