Skip to content

Commit

Permalink
fix: module locale option generation breaking SEO (nuxt-modules#2598)
Browse files Browse the repository at this point in the history
* fix: module locale option generation breaking SEO

* test: add issue nuxt-modules#2590 test
Rate limit · GitHub

Whoa there!

You have triggered an abuse detection mechanism.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

BobbieGoede authored Dec 6, 2023
1 parent 872a23c commit be4fdff
Showing 8 changed files with 133 additions and 5 deletions.
43 changes: 43 additions & 0 deletions specs/fixtures/issues/2590/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<template>
<div>
{{ $t('welcome') }}
</div>
</template>

<script setup lang="ts">
const { locale, locales, setLocale } = useI18n()
const head = useLocaleHead({
addDirAttribute: true,
addSeoAttributes: true
})
useHead({
htmlAttrs: head.value.htmlAttrs
})
console.log(head.value.htmlAttrs)
const availableLocales = computed(() => {
return (locales.value as LocaleObject[])
.filter(item => {
return item.code !== locale.value
})
.map(item => {
return {
label: item.name,
key: item.code
}
})
})
const currentLocale = computed(() => {
return (locales.value as LocaleObject[]).filter(item => {
return item.code === locale.value
})
})
function changeLocal(code: any) {
setLocale(code)
}
</script>
3 changes: 3 additions & 0 deletions specs/fixtures/issues/2590/i18n.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default defineI18nConfig(() => ({
legacy: false
}))
35 changes: 35 additions & 0 deletions specs/fixtures/issues/2590/modules/i18n-module/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { createResolver, defineNuxtModule, installModule } from '@nuxt/kit'

export default defineNuxtModule({
async setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url)

nuxt.hook('i18n:registerModule', registerModule =>
registerModule({
langDir: resolve('lang'),
locales: [
{
code: 'en',
iso: 'en-US',
file: 'en-US.ts',
name: 'English'
}
]
})
)

await installModule('@nuxtjs/i18n', {
lazy: true,
defaultLocale: 'en',
experimental: {
jsTsFormatResource: true
},
detectBrowserLanguage: {
useCookie: true,
cookieKey: 'i18n_lang',
redirectOn: 'root'
},
vueI18n: './i18n.config.ts'
})
}
})
5 changes: 5 additions & 0 deletions specs/fixtures/issues/2590/modules/i18n-module/lang/en-US.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default defineI18nLocale(async () => {
return {
welcome: 'welcome translated'
}
})
5 changes: 5 additions & 0 deletions specs/fixtures/issues/2590/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
devtools: { enabled: false },
modules: ['./modules/i18n-module']
})
14 changes: 14 additions & 0 deletions specs/fixtures/issues/2590/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "nuxt3-test-issues-2473",
"private": true,
"scripts": {
"build": "nuxt build",
"dev": "nuxt dev",
"generate": "nuxt generate",
"preview": "nuxt preview"
},
"devDependencies": {
"@nuxtjs/i18n": "latest",
"nuxt": "latest"
}
}
22 changes: 22 additions & 0 deletions specs/issues/2590.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { test, expect, describe } from 'vitest'
import { fileURLToPath } from 'node:url'
import { URL } from 'node:url'
import { setup } from '../utils'
import { renderPage } from '../helper'

describe('#2590', async () => {
await setup({
rootDir: fileURLToPath(new URL(`../fixtures/issues/2590`, import.meta.url)),
browser: true
})

test('Locale ISO code is required to generate alternate link', async () => {
const { page } = await renderPage('/')

// html tag `lang` attribute
expect(await page.getAttribute('html', 'lang')).toMatch('en')

// html tag `dir` attribute
expect(await page.getAttribute('html', 'dir')).toMatch('ltr')
})
})
11 changes: 6 additions & 5 deletions src/gen.ts
Original file line number Diff line number Diff line change
@@ -25,11 +25,12 @@ const generateVueI18nConfiguration = (config: Required<VueI18nConfigPathInfo>):
})
}

function simplifyLocaleOptions(nuxt: Nuxt, locales: LocaleObject[]) {
const hasObjectLocales = nuxt.options._layers.some(
layer => layer?.config?.i18n?.locales?.some(x => typeof x !== 'string')
)
function simplifyLocaleOptions(nuxt: Nuxt, options: NuxtI18nOptions) {
const hasObjectLocales =
nuxt.options._layers.some(layer => layer?.config?.i18n?.locales?.some(x => typeof x !== 'string')) ||
options?.i18nModules?.some(module => module?.locales?.some(x => typeof x !== 'string'))

const locales = (options.locales ?? []) as LocaleObject[]
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return locales.map(({ meta, ...locale }) => {
if (!hasObjectLocales) {
@@ -94,7 +95,7 @@ export function generateLoaderOptions(nuxt: Nuxt, { nuxtI18nOptions, vueI18nConf

const generatedNuxtI18nOptions = {
...nuxtI18nOptions,
locales: simplifyLocaleOptions(nuxt, (nuxtI18nOptions?.locales ?? []) as unknown as LocaleObject[])
locales: simplifyLocaleOptions(nuxt, nuxtI18nOptions)
}
delete nuxtI18nOptions.vueI18n

0 comments on commit be4fdff

Please sign in to comment.