Skip to content

Commit dcd1f79

Browse files
Nabellaleenpaulgv
authored andcommitted
feat: Add canonical link to PREFIX_AND_DEFAULT duplicated pages (#194)
1 parent 909062f commit dcd1f79

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

docs/seo.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ By default, **nuxt-i18n** attempts to add some metadata to improve your pages SE
55
* Add a _lang_ attribute containing current locale's ISO code to the `<html>` tag.
66
* Generate `<link rel="alternate" hreflang="x">` tags for every language configured in `nuxt.config.js`. For each language, the ISO code is used as `hreflang` attribute's value. [More on hreflang](https://support.google.com/webmasters/answer/189077)
77
* Generate `og:locale` and `og:locale:alternate` meta tags as defined in the [Open Graph protocol](http://ogp.me/#optional)
8+
* When using `prefix_and_default` strategy, generate `rel="canonical"` link on the default language routes containing the
9+
prefix to avoid duplicate indexation. [More on canonical](https://support.google.com/webmasters/answer/182192#dup-content)
810

911

1012
For this feature to work, you must configure `locales` option as an array of objects, where each object has an `iso` option set to the language ISO code:

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
LOCALE_ISO_KEY,
1414
LOCALE_DOMAIN_KEY,
1515
LOCALE_FILE_KEY,
16+
STRATEGIES,
1617
COMPONENT_OPTIONS_KEY
1718
} = require('./helpers/constants')
1819

@@ -46,6 +47,7 @@ module.exports = function (userOptions) {
4647
LOCALE_ISO_KEY,
4748
LOCALE_DOMAIN_KEY,
4849
LOCALE_FILE_KEY,
50+
STRATEGIES,
4951
COMPONENT_OPTIONS_KEY,
5052
getLocaleCodes,
5153
getLocaleFromRoute,

src/templates/seo-head.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const nuxtI18nSeo = function () {
1212
const LOCALE_CODE_KEY = '<%= options.LOCALE_CODE_KEY %>'
1313
const LOCALE_ISO_KEY = '<%= options.LOCALE_ISO_KEY %>'
1414
const BASE_URL = '<%= options.baseUrl %>'
15+
const STRATEGY = '<%= options.strategy %>'
1516

1617
// Prepare html lang attribute
1718
const currentLocaleData = this.$i18n.locales.find(l => l[LOCALE_CODE_KEY] === this.$i18n.locale)
@@ -37,6 +38,19 @@ export const nuxtI18nSeo = function () {
3738
})
3839
.filter(item => !!item)
3940

41+
// canonical links
42+
if (STRATEGY === '<%= options.STRATEGIES.PREFIX_AND_DEFAULT %>') {
43+
const canonicalPath = this.switchLocalePath(currentLocaleData[LOCALE_CODE_KEY])
44+
if (canonicalPath && canonicalPath !== this.$route.path) {
45+
// Current page is not the canonical one -- add a canonical link
46+
link.push({
47+
hid: 'canonical-lang-' + currentLocaleData[LOCALE_CODE_KEY],
48+
rel: "canonical",
49+
href: BASE_URL + canonicalPath
50+
})
51+
}
52+
}
53+
4054
// og:locale meta
4155
const meta = []
4256
// og:locale - current

0 commit comments

Comments
 (0)