Skip to content

Commit

Permalink
fix($seo): Avoid duplicate description meta at runtime. (close: #665)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz committed Jul 27, 2018
1 parent 231da6a commit b207a5f
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions lib/app/root-mixins/updateMeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,43 @@ export default {
this.$ssrContext.description = this.$page.description || this.$description
}
},

mounted () {
// update title / meta tags
this.currentMetaTags = []
this.currentMetaTags = new Set()

const updateMeta = () => {
document.title = this.$title
document.documentElement.lang = this.$lang
const meta = [
{
name: 'description',
content: this.$description
},
...(this.$page.frontmatter.meta || [])
]
this.currentMetaTags = updateMetaTags(meta, this.currentMetaTags)
const userMeta = this.$page.frontmatter.meta || []
const meta = userMeta.slice(0)
const useGlobalDescription = userMeta.filter(m => m.name === 'description').length === 0

// #665 Avoid duplicate description meta at runtime.
if (useGlobalDescription) {
meta.push({ name: 'description', content: this.$description })
}

// Including description meta coming from SSR.
const descriptionMetas = document.querySelectorAll('meta[name="description"]')
if (descriptionMetas.length) {
descriptionMetas.forEach(m => this.currentMetaTags.add(m))
}

this.currentMetaTags = new Set(updateMetaTags(meta, this.currentMetaTags))
}
this.$watch('$page', updateMeta)
updateMeta()
},

beforeDestroy () {
updateMetaTags(null, this.currentMetaTags)
}
}

function updateMetaTags (meta, current) {
if (current) {
current.forEach(c => {
[...current].forEach(c => {
document.head.removeChild(c)
})
}
Expand Down

0 comments on commit b207a5f

Please sign in to comment.