Skip to content

Commit b207a5f

Browse files
committed
fix($seo): Avoid duplicate description meta at runtime. (close: #665)
1 parent 231da6a commit b207a5f

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

lib/app/root-mixins/updateMeta.js

+21-10
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,43 @@ export default {
66
this.$ssrContext.description = this.$page.description || this.$description
77
}
88
},
9+
910
mounted () {
1011
// update title / meta tags
11-
this.currentMetaTags = []
12+
this.currentMetaTags = new Set()
13+
1214
const updateMeta = () => {
1315
document.title = this.$title
1416
document.documentElement.lang = this.$lang
15-
const meta = [
16-
{
17-
name: 'description',
18-
content: this.$description
19-
},
20-
...(this.$page.frontmatter.meta || [])
21-
]
22-
this.currentMetaTags = updateMetaTags(meta, this.currentMetaTags)
17+
const userMeta = this.$page.frontmatter.meta || []
18+
const meta = userMeta.slice(0)
19+
const useGlobalDescription = userMeta.filter(m => m.name === 'description').length === 0
20+
21+
// #665 Avoid duplicate description meta at runtime.
22+
if (useGlobalDescription) {
23+
meta.push({ name: 'description', content: this.$description })
24+
}
25+
26+
// Including description meta coming from SSR.
27+
const descriptionMetas = document.querySelectorAll('meta[name="description"]')
28+
if (descriptionMetas.length) {
29+
descriptionMetas.forEach(m => this.currentMetaTags.add(m))
30+
}
31+
32+
this.currentMetaTags = new Set(updateMetaTags(meta, this.currentMetaTags))
2333
}
2434
this.$watch('$page', updateMeta)
2535
updateMeta()
2636
},
37+
2738
beforeDestroy () {
2839
updateMetaTags(null, this.currentMetaTags)
2940
}
3041
}
3142

3243
function updateMetaTags (meta, current) {
3344
if (current) {
34-
current.forEach(c => {
45+
[...current].forEach(c => {
3546
document.head.removeChild(c)
3647
})
3748
}

0 commit comments

Comments
 (0)