Skip to content

Commit

Permalink
fix: reactive lang when using Nuxt I18n
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Aug 7, 2024
1 parent 2005a29 commit 58e4f3e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 37 deletions.
7 changes: 1 addition & 6 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,7 @@ export default defineNuxtModule<ModuleOptions>({
// i18n complicates things, we need to run the server plugin at the right time, client is fine
if (hasNuxtModule('@nuxtjs/i18n')) {
addPlugin({
src: resolve(`./runtime/nuxt/plugin/defaultsWaitI18n.server`),
mode: 'server',
})
addPlugin({
src: resolve(`./runtime/nuxt/plugin/defaults`),
mode: 'client',
src: resolve(`./runtime/nuxt/plugin/defaultsWaitI18n`),
})
}
else {
Expand Down
19 changes: 5 additions & 14 deletions src/runtime/nuxt/logic/applyDefaults.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import type { UseHeadOptions, UseSeoMetaInput } from '@unhead/vue'
import type { QueryObject } from 'ufo'
import { stringifyQuery } from 'ufo'
import type { Ref } from 'vue'
import {
computed,
createSitePathResolver,
useHead,
useRoute,
useRuntimeConfig,
useSeoMeta,
useServerHead,
useSiteConfig,
} from '#imports'

export function applyDefaults() {
export function applyDefaults(i18n: { locale: Ref<string> }) {
// get the head instance
const { canonicalQueryWhitelist } = useRuntimeConfig().public['nuxt-seo']
const siteConfig = useSiteConfig()
Expand All @@ -37,25 +37,16 @@ export function applyDefaults() {

// needs higher priority
useHead({
link: [{ rel: 'canonical', href: () => canonicalUrl.value }],
}, minimalPriority)
const locale = siteConfig.currentLocale || siteConfig.defaultLocale
if (locale) {
useServerHead({
htmlAttrs: { lang: locale },
})
}

// TODO support SPA
useHead({
htmlAttrs: { lang: i18n.locale },
templateParams: { site: siteConfig, siteName: siteConfig.name || '' },
titleTemplate: '%s %separator %siteName',
link: [{ rel: 'canonical', href: () => canonicalUrl.value }],
}, minimalPriority)

const seoMeta: UseSeoMetaInput = {
ogType: 'website',
ogUrl: () => canonicalUrl.value,
ogLocale: locale,
ogLocale: () => i18n.locale.value,
ogSiteName: siteConfig.name,
}
if (siteConfig.description)
Expand Down
14 changes: 12 additions & 2 deletions src/runtime/nuxt/plugin/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { applyDefaults as setup } from '../logic/applyDefaults'
import { applyDefaults } from '../logic/applyDefaults'
import {
defineNuxtPlugin,
ref,
useSiteConfig,
} from '#imports'

export default defineNuxtPlugin({
name: 'nuxt-seo:defaults',
order: 999,
env: {
islands: false,
},
setup,
setup() {
const siteConfig = useSiteConfig()
const locale = ref(siteConfig.currentLocale || siteConfig.defaultLocale)
applyDefaults({
locale,
})
// TODO reactive locale, need upstream fixes to nuxt-site-config
},
})
15 changes: 0 additions & 15 deletions src/runtime/nuxt/plugin/defaultsWaitI18n.server.ts

This file was deleted.

24 changes: 24 additions & 0 deletions src/runtime/nuxt/plugin/defaultsWaitI18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { applyDefaults } from '../logic/applyDefaults'
import { defineNuxtPlugin, ref } from '#imports'

export default defineNuxtPlugin({
name: 'nuxt-seo:defaults',
env: {
islands: false,
},
// we need to wait for the i18n plugin to run first
// @ts-expect-error dynamic
dependsOn: import.meta.server
? [
'nuxt-site-config:i18n',
]
: [],
setup(nuxtApp) {
const locale = ref(nuxtApp.$i18n!.locale.value)
// @ts-expect-error untyped
nuxtApp.hook('i18n:localeSwitched', ({ newLocale }) => {
locale.value = newLocale
})
applyDefaults({ locale })
},
})
5 changes: 5 additions & 0 deletions test/fixtures/i18n/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ export default defineNuxtConfig({
'@nuxtjs/i18n',
'@nuxt/ui',
],

site: {
url: 'https://nuxtseo.com',
debug: true,
},

nitro: {
prerender: {
failOnError: false,
ignore: ['/'],
},
},

i18n: {
baseUrl: 'https://nuxtseo.com',
defaultLocale: 'en',
Expand All @@ -40,4 +43,6 @@ export default defineNuxtConfig({
},
],
},

compatibilityDate: '2024-08-07',
})

0 comments on commit 58e4f3e

Please sign in to comment.