Skip to content

Commit

Permalink
fix: add x-default hreflang tag for improved SEO (#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
divine authored Oct 12, 2020
1 parent 830ced3 commit b35bc57
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/templates/seo-head.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import VueMeta from 'vue-meta'
import {
defaultLocale,
COMPONENT_OPTIONS_KEY,
LOCALE_CODE_KEY,
LOCALE_ISO_KEY,
Expand Down Expand Up @@ -74,6 +75,15 @@ function addHreflangLinks (locales, baseUrl, link) {
hreflang: iso
})
}

if (defaultLocale) {
link.push({
hid: 'alternate-x-default',
rel: 'alternate',
href: baseUrl + this.switchLocalePath(defaultLocale),
hreflang: 'x-default'
})
}
}

function addCanonicalLinks (currentLocale, baseUrl, link) {
Expand Down
24 changes: 24 additions & 0 deletions test/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ for (const trailingSlash of TRAILING_SLASHES) {
rel: 'alternate',
href: pathRespectingTrailingSlash('nuxt-app.localhost/fr'),
hreflang: 'fr-FR'
},
{
tagName: 'link',
rel: 'alternate',
href: 'nuxt-app.localhost/',
hreflang: 'x-default'
}
]

Expand Down Expand Up @@ -585,6 +591,12 @@ for (const trailingSlash of TRAILING_SLASHES) {
rel: 'alternate',
href: pathRespectingTrailingSlash('nuxt-app.localhost/fr/loader-yaml'),
hreflang: 'fr-FR'
},
{
tagName: 'link',
rel: 'alternate',
href: pathRespectingTrailingSlash('nuxt-app.localhost/loader-yaml'),
hreflang: 'x-default'
}
]

Expand Down Expand Up @@ -689,6 +701,12 @@ describe('hreflang', () => {
hreflang: 'es-VE',
rel: 'alternate',
tagName: 'link'
},
{
href: 'nuxt-app.localhost/',
hreflang: 'x-default',
rel: 'alternate',
tagName: 'link'
}
]

Expand Down Expand Up @@ -1233,6 +1251,12 @@ describe('baseUrl', () => {
href: 'CUSTOM/fr?noncanonical',
hreflang: 'fr-FR'
},
{
tagName: 'link',
rel: 'alternate',
href: 'CUSTOM/?noncanonical',
hreflang: 'x-default'
},
{
tagName: 'link',
rel: 'canonical',
Expand Down

0 comments on commit b35bc57

Please sign in to comment.