-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure defaults are applied after i18n site config
Fixes #199
- Loading branch information
Showing
8 changed files
with
107 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { UseHeadOptions, UseSeoMetaInput } from '@unhead/vue' | ||
import { | ||
computed, | ||
createSitePathResolver, | ||
useHead, | ||
useRoute, | ||
useSeoMeta, | ||
useServerHead, | ||
useSiteConfig, | ||
} from '#imports' | ||
|
||
export function applyDefaults() { | ||
// get the head instance | ||
const siteConfig = useSiteConfig() | ||
const route = useRoute() | ||
const resolveUrl = createSitePathResolver({ withBase: true, absolute: true }) | ||
const canonicalUrl = computed<string>(() => resolveUrl(route.path || '/').value || route.path) | ||
|
||
const minimalPriority: UseHeadOptions = { | ||
// give nuxt.config values higher priority | ||
tagPriority: 101, | ||
} | ||
|
||
// needs higher priority | ||
useHead({ | ||
link: [{ rel: 'canonical', href: () => canonicalUrl.value }], | ||
}) | ||
const locale = siteConfig.currentLocale || siteConfig.defaultLocale | ||
if (locale) { | ||
useServerHead({ | ||
htmlAttrs: { lang: locale }, | ||
}) | ||
} | ||
|
||
// TODO support SPA | ||
useHead({ | ||
templateParams: { site: siteConfig, siteName: siteConfig.name || '' }, | ||
titleTemplate: '%s %separator %siteName', | ||
}, minimalPriority) | ||
|
||
const seoMeta: UseSeoMetaInput = { | ||
ogType: 'website', | ||
ogUrl: () => canonicalUrl.value, | ||
ogLocale: locale, | ||
ogSiteName: siteConfig.name, | ||
} | ||
if (siteConfig.description) | ||
seoMeta.description = siteConfig.description | ||
if (siteConfig.twitter) { | ||
// id must have the @ in it | ||
const id = siteConfig.twitter.startsWith('@') | ||
? siteConfig.twitter | ||
: `@${siteConfig.twitter}` | ||
seoMeta.twitterCreator = id | ||
seoMeta.twitterSite = id | ||
} | ||
// TODO server only for some tags | ||
useSeoMeta(seoMeta, minimalPriority) | ||
} |
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,66 +1,12 @@ | ||
import type { UseHeadOptions, UseSeoMetaInput } from '@unhead/vue' | ||
import { applyDefaults as setup } from '../logic/applyDefaults' | ||
import { | ||
computed, | ||
createSitePathResolver, | ||
defineNuxtPlugin, | ||
useHead, | ||
useRoute, | ||
useSeoMeta, | ||
useServerHead, | ||
useSiteConfig, | ||
} from '#imports' | ||
|
||
export default defineNuxtPlugin({ | ||
name: 'nuxt-seo:defaults', | ||
env: { | ||
islands: false, | ||
}, | ||
setup() { | ||
// get the head instance | ||
const siteConfig = useSiteConfig() | ||
const route = useRoute() | ||
const resolveUrl = createSitePathResolver({ withBase: true, absolute: true }) | ||
const canonicalUrl = computed<string>(() => resolveUrl(route.path || '/').value || route.path) | ||
|
||
const minimalPriority: UseHeadOptions = { | ||
// give nuxt.config values higher priority | ||
tagPriority: 101, | ||
} | ||
|
||
// needs higher priority | ||
useHead({ | ||
link: [{ rel: 'canonical', href: () => canonicalUrl.value }], | ||
}) | ||
const locale = siteConfig.currentLocale || siteConfig.defaultLocale | ||
if (locale) { | ||
useServerHead({ | ||
htmlAttrs: { lang: locale }, | ||
}) | ||
} | ||
|
||
// TODO support SPA | ||
useHead({ | ||
templateParams: { site: siteConfig, siteName: siteConfig.name || '' }, | ||
titleTemplate: '%s %separator %siteName', | ||
}, minimalPriority) | ||
|
||
const seoMeta: UseSeoMetaInput = { | ||
ogType: 'website', | ||
ogUrl: () => canonicalUrl.value, | ||
ogLocale: locale, | ||
ogSiteName: siteConfig.name, | ||
} | ||
if (siteConfig.description) | ||
seoMeta.description = siteConfig.description | ||
if (siteConfig.twitter) { | ||
// id must have the @ in it | ||
const id = siteConfig.twitter.startsWith('@') | ||
? siteConfig.twitter | ||
: `@${siteConfig.twitter}` | ||
seoMeta.twitterCreator = id | ||
seoMeta.twitterSite = id | ||
} | ||
// TODO server only for some tags | ||
useSeoMeta(seoMeta, minimalPriority) | ||
}, | ||
setup, | ||
}) |
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,15 @@ | ||
import { applyDefaults as setup } from '../logic/applyDefaults' | ||
import { defineNuxtPlugin } from '#imports' | ||
|
||
export default defineNuxtPlugin({ | ||
name: 'nuxt-seo:defaults', | ||
env: { | ||
islands: false, | ||
}, | ||
// we need to wait for the i18n plugin to run first | ||
dependsOn: [ | ||
// @ts-expect-error dynamic | ||
'nuxt-site-config:i18n', | ||
], | ||
setup, | ||
}) |
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,3 +1,6 @@ | ||
export default { | ||
welcome: 'Welcome', | ||
nuxtSiteConfig: { | ||
description: 'en description', | ||
}, | ||
} |
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,3 +1,6 @@ | ||
export default { | ||
welcome: 'ようこそ', | ||
nuxtSiteConfig: { | ||
description: 'es description', | ||
}, | ||
} |
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,3 +1,7 @@ | ||
export default { | ||
welcome: 'ようこそ', | ||
nuxtSiteConfig: { | ||
name: 'fr name', | ||
description: 'fr description', | ||
}, | ||
} |
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