Skip to content

Commit

Permalink
feat: Add canonical link to PREFIX_AND_DEFAULT duplicated pages (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nabellaleen authored and paulgv committed Jan 20, 2019
1 parent 909062f commit dcd1f79
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/seo.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ By default, **nuxt-i18n** attempts to add some metadata to improve your pages SE
* Add a _lang_ attribute containing current locale's ISO code to the `<html>` tag.
* 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)
* Generate `og:locale` and `og:locale:alternate` meta tags as defined in the [Open Graph protocol](http://ogp.me/#optional)
* When using `prefix_and_default` strategy, generate `rel="canonical"` link on the default language routes containing the
prefix to avoid duplicate indexation. [More on canonical](https://support.google.com/webmasters/answer/182192#dup-content)


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:
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
LOCALE_ISO_KEY,
LOCALE_DOMAIN_KEY,
LOCALE_FILE_KEY,
STRATEGIES,
COMPONENT_OPTIONS_KEY
} = require('./helpers/constants')

Expand Down Expand Up @@ -46,6 +47,7 @@ module.exports = function (userOptions) {
LOCALE_ISO_KEY,
LOCALE_DOMAIN_KEY,
LOCALE_FILE_KEY,
STRATEGIES,
COMPONENT_OPTIONS_KEY,
getLocaleCodes,
getLocaleFromRoute,
Expand Down
14 changes: 14 additions & 0 deletions src/templates/seo-head.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const nuxtI18nSeo = function () {
const LOCALE_CODE_KEY = '<%= options.LOCALE_CODE_KEY %>'
const LOCALE_ISO_KEY = '<%= options.LOCALE_ISO_KEY %>'
const BASE_URL = '<%= options.baseUrl %>'
const STRATEGY = '<%= options.strategy %>'

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

// canonical links
if (STRATEGY === '<%= options.STRATEGIES.PREFIX_AND_DEFAULT %>') {
const canonicalPath = this.switchLocalePath(currentLocaleData[LOCALE_CODE_KEY])
if (canonicalPath && canonicalPath !== this.$route.path) {
// Current page is not the canonical one -- add a canonical link
link.push({
hid: 'canonical-lang-' + currentLocaleData[LOCALE_CODE_KEY],
rel: "canonical",
href: BASE_URL + canonicalPath
})
}
}

// og:locale meta
const meta = []
// og:locale - current
Expand Down

0 comments on commit dcd1f79

Please sign in to comment.