From 1caa07ec07f4b3dc629f7af9a2605e8fc2a4c670 Mon Sep 17 00:00:00 2001 From: Derek Pollard Date: Mon, 12 Oct 2020 17:24:14 -0500 Subject: [PATCH 1/7] feat($core): add canonical link to frontmatter --- .../@vuepress/core/lib/client/index.ssr.html | 1 + .../core/lib/client/root-mixins/updateMeta.js | 23 +++++++++++++++++++ .../core/lib/node/ClientComputedMixin.js | 10 ++++++++ 3 files changed, 34 insertions(+) diff --git a/packages/@vuepress/core/lib/client/index.ssr.html b/packages/@vuepress/core/lib/client/index.ssr.html index 351c008efe..594544dfa3 100644 --- a/packages/@vuepress/core/lib/client/index.ssr.html +++ b/packages/@vuepress/core/lib/client/index.ssr.html @@ -7,6 +7,7 @@ {{{ userHeadTags }}} {{{ pageMeta }}} + {{{ canonical() }}} {{{ renderResourceHints() }}} {{{ renderStyles() }}} diff --git a/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js b/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js index 485f544d38..ce8fca7a4c 100644 --- a/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js +++ b/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js @@ -13,6 +13,7 @@ export default { this.$ssrContext.title = this.$title this.$ssrContext.lang = this.$lang this.$ssrContext.pageMeta = renderPageMeta(mergedMetaItems) + this.$ssrContext.canonical = renderCanonical(this.$canonical) } }, // Other life cycles will only be called at client @@ -22,6 +23,7 @@ export default { // update title / meta tags this.updateMeta() + this.updateCanonical() }, methods: { @@ -39,6 +41,20 @@ export default { // description needs special attention as it has too many entries return unionBy([{ name: 'description', content: this.$description }], pageMeta, this.siteMeta, metaIdentifier) + }, + + updateCanonical () { + const canonicalEl = document.querySelector("link[rel='canonical']") + + if (canonicalEl) { + canonicalEl.remove() + } + + if (!this.$canonical) { + return + } + + document.head.insertAdjacentHTML('beforeend', renderCanonical(this.$canonical)) } }, @@ -53,6 +69,13 @@ export default { } } +function renderCanonical (link = '') { + if (!link) { + return '' + } + return `` +} + /** * Replace currentMetaTags with newMetaTags * @param {Array} newMetaTags diff --git a/packages/@vuepress/core/lib/node/ClientComputedMixin.js b/packages/@vuepress/core/lib/node/ClientComputedMixin.js index dbe8a3f7a5..d40c3a9902 100644 --- a/packages/@vuepress/core/lib/node/ClientComputedMixin.js +++ b/packages/@vuepress/core/lib/node/ClientComputedMixin.js @@ -65,6 +65,16 @@ module.exports = siteData => { return this.$localeConfig.title || this.$site.title || '' } + get $canonical () { + const { canonical } = this.$page.frontmatter + + if (typeof canonical === 'string') { + return canonical + } + + return false + } + get $title () { const page = this.$page const { metaTitle } = this.$page.frontmatter From 9844cb489ec7737e2e806c0ec6aea071763228c0 Mon Sep 17 00:00:00 2001 From: Derek Pollard Date: Mon, 12 Oct 2020 17:32:08 -0500 Subject: [PATCH 2/7] feat($core): add canonical link to frontmatter --- packages/@vuepress/core/lib/client/index.ssr.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@vuepress/core/lib/client/index.ssr.html b/packages/@vuepress/core/lib/client/index.ssr.html index 594544dfa3..503104e69d 100644 --- a/packages/@vuepress/core/lib/client/index.ssr.html +++ b/packages/@vuepress/core/lib/client/index.ssr.html @@ -7,7 +7,7 @@ {{{ userHeadTags }}} {{{ pageMeta }}} - {{{ canonical() }}} + {{{ canonical }}} {{{ renderResourceHints() }}} {{{ renderStyles() }}} From b6d530b90cde4d894e354319b88328e10ee28166 Mon Sep 17 00:00:00 2001 From: Derek Pollard Date: Mon, 12 Oct 2020 19:45:42 -0500 Subject: [PATCH 3/7] feat($core): add canonical link to frontmatter --- packages/@vuepress/core/lib/client/root-mixins/updateMeta.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js b/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js index ce8fca7a4c..f4801884e6 100644 --- a/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js +++ b/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js @@ -61,6 +61,7 @@ export default { watch: { $page () { this.updateMeta() + this.updateCanonical() } }, From 7d6b759ec223beac75511ac10a96a816b49f50d5 Mon Sep 17 00:00:00 2001 From: Derek Pollard Date: Mon, 12 Oct 2020 20:59:49 -0500 Subject: [PATCH 4/7] feat($core): add canonical link to frontmatter --- .../@vuepress/core/lib/client/index.ssr.html | 2 +- .../core/lib/client/root-mixins/updateMeta.js | 23 +++++++++++-------- .../core/lib/node/ClientComputedMixin.js | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/@vuepress/core/lib/client/index.ssr.html b/packages/@vuepress/core/lib/client/index.ssr.html index 503104e69d..5888234d40 100644 --- a/packages/@vuepress/core/lib/client/index.ssr.html +++ b/packages/@vuepress/core/lib/client/index.ssr.html @@ -7,7 +7,7 @@ {{{ userHeadTags }}} {{{ pageMeta }}} - {{{ canonical }}} + {{{ canonicalLink }}} {{{ renderResourceHints() }}} {{{ renderStyles() }}} diff --git a/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js b/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js index f4801884e6..ae5dbcdbf4 100644 --- a/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js +++ b/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js @@ -13,7 +13,7 @@ export default { this.$ssrContext.title = this.$title this.$ssrContext.lang = this.$lang this.$ssrContext.pageMeta = renderPageMeta(mergedMetaItems) - this.$ssrContext.canonical = renderCanonical(this.$canonical) + this.$ssrContext.canonicalLink = renderCanonicalLink(this.$canonicalUrl) } }, // Other life cycles will only be called at client @@ -44,17 +44,13 @@ export default { }, updateCanonical () { - const canonicalEl = document.querySelector("link[rel='canonical']") + removeCanonicalLink() - if (canonicalEl) { - canonicalEl.remove() - } - - if (!this.$canonical) { + if (!this.$canonicalUrl) { return } - document.head.insertAdjacentHTML('beforeend', renderCanonical(this.$canonical)) + document.head.insertAdjacentHTML('beforeend', renderCanonicalLink(this.$canonicalUrl)) } }, @@ -67,10 +63,19 @@ export default { beforeDestroy () { updateMetaTags(null, this.currentMetaTags) + removeCanonicalLink() + } +} + +function removeCanonicalLink () { + const canonicalEl = document.querySelector("link[rel='canonical']") + + if (canonicalEl) { + canonicalEl.remove() } } -function renderCanonical (link = '') { +function renderCanonicalLink (link = '') { if (!link) { return '' } diff --git a/packages/@vuepress/core/lib/node/ClientComputedMixin.js b/packages/@vuepress/core/lib/node/ClientComputedMixin.js index d40c3a9902..2ea38bbddc 100644 --- a/packages/@vuepress/core/lib/node/ClientComputedMixin.js +++ b/packages/@vuepress/core/lib/node/ClientComputedMixin.js @@ -65,7 +65,7 @@ module.exports = siteData => { return this.$localeConfig.title || this.$site.title || '' } - get $canonical () { + get $canonicalUrl () { const { canonical } = this.$page.frontmatter if (typeof canonical === 'string') { From 82352edd45da9b49f355a38c02742194ed5904bb Mon Sep 17 00:00:00 2001 From: Derek Pollard Date: Mon, 12 Oct 2020 21:03:14 -0500 Subject: [PATCH 5/7] feat($core): make canonical link/url more idiomatic in context --- .../@vuepress/core/lib/client/root-mixins/updateMeta.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js b/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js index ae5dbcdbf4..c2265702f2 100644 --- a/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js +++ b/packages/@vuepress/core/lib/client/root-mixins/updateMeta.js @@ -23,7 +23,7 @@ export default { // update title / meta tags this.updateMeta() - this.updateCanonical() + this.updateCanonicalLink() }, methods: { @@ -43,7 +43,7 @@ export default { pageMeta, this.siteMeta, metaIdentifier) }, - updateCanonical () { + updateCanonicalLink () { removeCanonicalLink() if (!this.$canonicalUrl) { @@ -57,7 +57,7 @@ export default { watch: { $page () { this.updateMeta() - this.updateCanonical() + this.updateCanonicalLink() } }, From de11d8af451babd35d3aa8e11f90d5e9e6dac53d Mon Sep 17 00:00:00 2001 From: Derek Pollard Date: Tue, 13 Oct 2020 09:17:50 -0500 Subject: [PATCH 6/7] feat($core): document canonical url in frontmatter --- packages/docs/docs/guide/frontmatter.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/docs/docs/guide/frontmatter.md b/packages/docs/docs/guide/frontmatter.md index 39a0d2d76b..337829e8f1 100644 --- a/packages/docs/docs/guide/frontmatter.md +++ b/packages/docs/docs/guide/frontmatter.md @@ -110,6 +110,13 @@ meta: --- ``` +### canonicalUrl + +- Type: `string` +- Default: `undefined` + +Set the canonical url for the current page. + ## Predefined Variables Powered By Default Theme ### navbar From ca6ed2b690f818c82c07fab679faa2173e501bfc Mon Sep 17 00:00:00 2001 From: Derek Pollard Date: Tue, 13 Oct 2020 09:24:20 -0500 Subject: [PATCH 7/7] feat($core): document canonical url in frontmatter --- packages/docs/docs/guide/frontmatter.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docs/docs/guide/frontmatter.md b/packages/docs/docs/guide/frontmatter.md index 337829e8f1..5fa3718476 100644 --- a/packages/docs/docs/guide/frontmatter.md +++ b/packages/docs/docs/guide/frontmatter.md @@ -115,7 +115,7 @@ meta: - Type: `string` - Default: `undefined` -Set the canonical url for the current page. +Set the canonical URL for the current page. ## Predefined Variables Powered By Default Theme