From 2312258e1b156d3428f7e780ff8ded8c87e2d770 Mon Sep 17 00:00:00 2001 From: ipesic Date: Wed, 21 Aug 2024 21:26:50 +0200 Subject: [PATCH] fix(i18n): reverse only locales logic --- src/module.ts | 9 +- src/util/i18n.ts | 6 +- .../i18n/pages.only-locales.test.ts | 126 ++++++++++++++++++ 3 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 test/integration/i18n/pages.only-locales.test.ts diff --git a/src/module.ts b/src/module.ts index 557dea8b..e6c992f5 100644 --- a/src/module.ts +++ b/src/module.ts @@ -36,7 +36,7 @@ import { includesSitemapRoot, isNuxtGenerate, setupPrerenderHandler } from './pr import { mergeOnKey } from './runtime/utils-pure' import { setupDevToolsUI } from './devtools' import { normaliseDate } from './runtime/nitro/sitemap/urlset/normalise' -import { generatePathForI18nPages, getExcludedLocalesFromI18nConfig, splitPathForI18nLocales } from './util/i18n' +import { generatePathForI18nPages, getOnlyLocalesFromI18nConfig, splitPathForI18nLocales } from './util/i18n' import { normalizeFilters } from './util/filter' export interface ModuleOptions extends _ModuleOptions {} @@ -163,8 +163,11 @@ export default defineNuxtModule({ if (!await hasNuxtModuleCompatibility('@nuxtjs/i18n', '>=8')) logger.warn(`You are using @nuxtjs/i18n v${i18nVersion}. For the best compatibility, please upgrade to @nuxtjs/i18n v8.0.0 or higher.`) nuxtI18nConfig = (await getNuxtModuleOptions('@nuxtjs/i18n') || {}) as NuxtI18nOptions - const excludedLocales = getExcludedLocalesFromI18nConfig(nuxtI18nConfig) - normalisedLocales = mergeOnKey((nuxtI18nConfig.locales || []).map((locale: any) => typeof locale === 'string' ? { code: locale } : locale), 'code').filter((locale: NormalisedLocale) => !excludedLocales.includes(locale.code)) + normalisedLocales = mergeOnKey((nuxtI18nConfig.locales || []).map((locale: any) => typeof locale === 'string' ? { code: locale } : locale), 'code') + const onlyLocales = getOnlyLocalesFromI18nConfig(nuxtI18nConfig) + if (onlyLocales.length) { + normalisedLocales = normalisedLocales.filter((locale: NormalisedLocale) => onlyLocales.includes(locale.code)) + } usingI18nPages = !!Object.keys(nuxtI18nConfig.pages || {}).length if (usingI18nPages && !hasDisabledAutoI18n) { const i18nPagesSources: SitemapSourceBase = { diff --git a/src/util/i18n.ts b/src/util/i18n.ts index b87cad0f..6bc4585c 100644 --- a/src/util/i18n.ts +++ b/src/util/i18n.ts @@ -27,11 +27,11 @@ export function splitPathForI18nLocales(path: FilterInput, autoI18n: AutoI18nCon ] } -export function getExcludedLocalesFromI18nConfig(nuxtI18nConfig: NuxtI18nOptions) { +export function getOnlyLocalesFromI18nConfig(nuxtI18nConfig: NuxtI18nOptions) { const onlyLocales = nuxtI18nConfig?.bundle?.onlyLocales if (!onlyLocales) return [] - const excludedLocales = typeof onlyLocales === 'string' ? [onlyLocales] : onlyLocales - return excludedLocales + const includedLocales = typeof onlyLocales === 'string' ? [onlyLocales] : onlyLocales + return includedLocales } export function generatePathForI18nPages(ctx: StrategyProps): string { diff --git a/test/integration/i18n/pages.only-locales.test.ts b/test/integration/i18n/pages.only-locales.test.ts new file mode 100644 index 00000000..920e87f5 --- /dev/null +++ b/test/integration/i18n/pages.only-locales.test.ts @@ -0,0 +1,126 @@ +import { describe, expect, it } from 'vitest' +import { createResolver } from '@nuxt/kit' +import { $fetch, setup } from '@nuxt/test-utils' + +const { resolve } = createResolver(import.meta.url) + +await setup({ + rootDir: resolve('../../fixtures/i18n'), + server: true, + nuxtConfig: { + i18n: { + locales: [ + { + code: 'en', + iso: 'en-US', + }, + { + code: 'es', + iso: 'es-ES', + }, + { + code: 'fr', + iso: 'fr-FR', + }, + ], + bundle: { + onlyLocales: ['en', 'fr'], + }, + pages: { + 'about': { + en: '/about', + fr: '/a-propos', + }, + 'services/index': { + en: '/services', + fr: '/offres', + }, + 'services/development/index': { + en: '/services/development', + fr: '/offres/developement', + }, + 'services/development/app/index': { + en: '/services/development/app', + fr: '/offres/developement/app', + }, + 'services/development/website/index': { + en: '/services/development/website', + fr: '/offres/developement/site-web', + }, + 'services/coaching/index': { + en: '/services/coaching', + fr: '/offres/formation', + }, + 'random': { + en: '/random', + fr: false, + }, + }, + }, + }, +}) +describe('i18n pages only locale', () => { + it('basic', async () => { + const index = await $fetch('/sitemap_index.xml') + expect(index).toMatchInlineSnapshot(` + " + + + https://nuxtseo.com/__sitemap__/en-US.xml + + + https://nuxtseo.com/__sitemap__/fr-FR.xml + + " + `) + const fr = await $fetch('/__sitemap__/fr-FR.xml') + expect(fr).toMatchInlineSnapshot(` + " + + + https://nuxtseo.com/fr/a-propos + + + + + + https://nuxtseo.com/fr/offres + + + + + + https://nuxtseo.com/fr/__sitemap/url + weekly + + + + + + https://nuxtseo.com/fr/offres/developement + + + + + + https://nuxtseo.com/fr/offres/formation + + + + + + https://nuxtseo.com/fr/offres/developement/app + + + + + + https://nuxtseo.com/fr/offres/developement/site-web + + + + + " + `) + }, 60000) +})