-
-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose head SEO function to use in layout (#154)
- Loading branch information
1 parent
d1bbc84
commit ce373c4
Showing
5 changed files
with
159 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,6 @@ | ||
import Vue from 'vue' | ||
import { nuxtI18nSeo } from './seo-head' | ||
|
||
Vue.mixin({ | ||
head () { | ||
const COMPONENT_OPTIONS_KEY = '<%= options.COMPONENT_OPTIONS_KEY %>' | ||
if ( | ||
!this._hasMetaInfo || | ||
!this.$i18n || | ||
!this.$i18n.locales || | ||
this.$options[COMPONENT_OPTIONS_KEY] === false || | ||
(this.$options[COMPONENT_OPTIONS_KEY] && this.$options[COMPONENT_OPTIONS_KEY].seo === false) | ||
) { | ||
return {}; | ||
} | ||
const LOCALE_CODE_KEY = '<%= options.LOCALE_CODE_KEY %>' | ||
const LOCALE_ISO_KEY = '<%= options.LOCALE_ISO_KEY %>' | ||
const BASE_URL = '<%= options.baseUrl %>' | ||
|
||
// Prepare html lang attribute | ||
const currentLocaleData = this.$i18n.locales.find(l => l[LOCALE_CODE_KEY] === this.$i18n.locale) | ||
const htmlAttrs = {} | ||
if (currentLocaleData && currentLocaleData[LOCALE_ISO_KEY]) { | ||
htmlAttrs.lang = currentLocaleData[LOCALE_ISO_KEY] | ||
} | ||
|
||
// hreflang tags | ||
const link = this.$i18n.locales | ||
.map(locale => { | ||
if (locale[LOCALE_ISO_KEY]) { | ||
return { | ||
hid: 'alternate-hreflang-' + locale[LOCALE_ISO_KEY], | ||
rel: 'alternate', | ||
href: BASE_URL + this.switchLocalePath(locale.code), | ||
hreflang: locale[LOCALE_ISO_KEY] | ||
} | ||
} else { | ||
console.warn('[<%= options.MODULE_NAME %>] Locale ISO code is required to generate alternate link') | ||
return null | ||
} | ||
}) | ||
.filter(item => !!item) | ||
|
||
// og:locale meta | ||
const meta = [] | ||
// og:locale - current | ||
if (currentLocaleData && currentLocaleData[LOCALE_ISO_KEY]) { | ||
meta.push({ | ||
hid: 'og:locale', | ||
name: 'og:locale', | ||
property: 'og:locale', | ||
// Replace dash with underscore as defined in spec: language_TERRITORY | ||
content: currentLocaleData[LOCALE_ISO_KEY].replace(/-/g, '_') | ||
}) | ||
} | ||
// og:locale - alternate | ||
meta.push( | ||
...this.$i18n.locales | ||
.filter(l => l[LOCALE_ISO_KEY] && l[LOCALE_ISO_KEY] !== currentLocaleData[LOCALE_ISO_KEY]) | ||
.map(locale => ({ | ||
hid: 'og:locale:alternate-' + locale[LOCALE_ISO_KEY], | ||
name: 'og:locale:alternate', | ||
property: 'og:locale:alternate', | ||
content: locale[LOCALE_ISO_KEY].replace(/-/g, '_') | ||
})) | ||
); | ||
|
||
return { | ||
htmlAttrs, | ||
link, | ||
meta | ||
} | ||
} | ||
head: nuxtI18nSeo | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
export const nuxtI18nSeo = function () { | ||
const COMPONENT_OPTIONS_KEY = '<%= options.COMPONENT_OPTIONS_KEY %>' | ||
if ( | ||
!this._hasMetaInfo || | ||
!this.$i18n || | ||
!this.$i18n.locales || | ||
this.$options[COMPONENT_OPTIONS_KEY] === false || | ||
(this.$options[COMPONENT_OPTIONS_KEY] && this.$options[COMPONENT_OPTIONS_KEY].seo === false) | ||
) { | ||
return {}; | ||
} | ||
const LOCALE_CODE_KEY = '<%= options.LOCALE_CODE_KEY %>' | ||
const LOCALE_ISO_KEY = '<%= options.LOCALE_ISO_KEY %>' | ||
const BASE_URL = '<%= options.baseUrl %>' | ||
|
||
// Prepare html lang attribute | ||
const currentLocaleData = this.$i18n.locales.find(l => l[LOCALE_CODE_KEY] === this.$i18n.locale) | ||
const htmlAttrs = {} | ||
if (currentLocaleData && currentLocaleData[LOCALE_ISO_KEY]) { | ||
htmlAttrs.lang = currentLocaleData[LOCALE_ISO_KEY] | ||
} | ||
|
||
// hreflang tags | ||
const link = this.$i18n.locales | ||
.map(locale => { | ||
if (locale[LOCALE_ISO_KEY]) { | ||
return { | ||
hid: 'alternate-hreflang-' + locale[LOCALE_ISO_KEY], | ||
rel: 'alternate', | ||
href: BASE_URL + this.switchLocalePath(locale.code), | ||
hreflang: locale[LOCALE_ISO_KEY] | ||
} | ||
} else { | ||
console.warn('[<%= options.MODULE_NAME %>] Locale ISO code is required to generate alternate link') | ||
return null | ||
} | ||
}) | ||
.filter(item => !!item) | ||
|
||
// og:locale meta | ||
const meta = [] | ||
// og:locale - current | ||
if (currentLocaleData && currentLocaleData[LOCALE_ISO_KEY]) { | ||
meta.push({ | ||
hid: 'og:locale', | ||
name: 'og:locale', | ||
property: 'og:locale', | ||
// Replace dash with underscore as defined in spec: language_TERRITORY | ||
content: currentLocaleData[LOCALE_ISO_KEY].replace(/-/g, '_') | ||
}) | ||
} | ||
// og:locale - alternate | ||
meta.push( | ||
...this.$i18n.locales | ||
.filter(l => l[LOCALE_ISO_KEY] && l[LOCALE_ISO_KEY] !== currentLocaleData[LOCALE_ISO_KEY]) | ||
.map(locale => ({ | ||
hid: 'og:locale:alternate-' + locale[LOCALE_ISO_KEY], | ||
name: 'og:locale:alternate', | ||
property: 'og:locale:alternate', | ||
content: locale[LOCALE_ISO_KEY].replace(/-/g, '_') | ||
})) | ||
); | ||
|
||
return { | ||
htmlAttrs, | ||
link, | ||
meta | ||
} | ||
} |
Oops, something went wrong.