Skip to content

Commit cc86a0c

Browse files
Xenossolitariusipesic
and
ipesic
authoredAug 22, 2024
fix(i18n): reverse only locales logic (#346)
Co-authored-by: ipesic <pesic.ivan@feg.eu>
1 parent 82f687c commit cc86a0c

File tree

3 files changed

+135
-6
lines changed

3 files changed

+135
-6
lines changed
 

‎src/module.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { includesSitemapRoot, isNuxtGenerate, setupPrerenderHandler } from './pr
3636
import { mergeOnKey } from './runtime/utils-pure'
3737
import { setupDevToolsUI } from './devtools'
3838
import { normaliseDate } from './runtime/nitro/sitemap/urlset/normalise'
39-
import { generatePathForI18nPages, getExcludedLocalesFromI18nConfig, splitPathForI18nLocales } from './util/i18n'
39+
import { generatePathForI18nPages, getOnlyLocalesFromI18nConfig, splitPathForI18nLocales } from './util/i18n'
4040
import { normalizeFilters } from './util/filter'
4141

4242
export interface ModuleOptions extends _ModuleOptions {}
@@ -163,8 +163,11 @@ export default defineNuxtModule<ModuleOptions>({
163163
if (!await hasNuxtModuleCompatibility('@nuxtjs/i18n', '>=8'))
164164
logger.warn(`You are using @nuxtjs/i18n v${i18nVersion}. For the best compatibility, please upgrade to @nuxtjs/i18n v8.0.0 or higher.`)
165165
nuxtI18nConfig = (await getNuxtModuleOptions('@nuxtjs/i18n') || {}) as NuxtI18nOptions
166-
const excludedLocales = getExcludedLocalesFromI18nConfig(nuxtI18nConfig)
167-
normalisedLocales = mergeOnKey((nuxtI18nConfig.locales || []).map((locale: any) => typeof locale === 'string' ? { code: locale } : locale), 'code').filter((locale: NormalisedLocale) => !excludedLocales.includes(locale.code))
166+
normalisedLocales = mergeOnKey((nuxtI18nConfig.locales || []).map((locale: any) => typeof locale === 'string' ? { code: locale } : locale), 'code')
167+
const onlyLocales = getOnlyLocalesFromI18nConfig(nuxtI18nConfig)
168+
if (onlyLocales.length) {
169+
normalisedLocales = normalisedLocales.filter((locale: NormalisedLocale) => onlyLocales.includes(locale.code))
170+
}
168171
usingI18nPages = !!Object.keys(nuxtI18nConfig.pages || {}).length
169172
if (usingI18nPages && !hasDisabledAutoI18n) {
170173
const i18nPagesSources: SitemapSourceBase = {

‎src/util/i18n.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ export function splitPathForI18nLocales(path: FilterInput, autoI18n: AutoI18nCon
2727
]
2828
}
2929

30-
export function getExcludedLocalesFromI18nConfig(nuxtI18nConfig: NuxtI18nOptions) {
30+
export function getOnlyLocalesFromI18nConfig(nuxtI18nConfig: NuxtI18nOptions) {
3131
const onlyLocales = nuxtI18nConfig?.bundle?.onlyLocales
3232
if (!onlyLocales) return []
33-
const excludedLocales = typeof onlyLocales === 'string' ? [onlyLocales] : onlyLocales
34-
return excludedLocales
33+
const includedLocales = typeof onlyLocales === 'string' ? [onlyLocales] : onlyLocales
34+
return includedLocales
3535
}
3636

3737
export function generatePathForI18nPages(ctx: StrategyProps): string {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { createResolver } from '@nuxt/kit'
3+
import { $fetch, setup } from '@nuxt/test-utils'
4+
5+
const { resolve } = createResolver(import.meta.url)
6+
7+
await setup({
8+
rootDir: resolve('../../fixtures/i18n'),
9+
server: true,
10+
nuxtConfig: {
11+
i18n: {
12+
locales: [
13+
{
14+
code: 'en',
15+
iso: 'en-US',
16+
},
17+
{
18+
code: 'es',
19+
iso: 'es-ES',
20+
},
21+
{
22+
code: 'fr',
23+
iso: 'fr-FR',
24+
},
25+
],
26+
bundle: {
27+
onlyLocales: ['en', 'fr'],
28+
},
29+
pages: {
30+
'about': {
31+
en: '/about',
32+
fr: '/a-propos',
33+
},
34+
'services/index': {
35+
en: '/services',
36+
fr: '/offres',
37+
},
38+
'services/development/index': {
39+
en: '/services/development',
40+
fr: '/offres/developement',
41+
},
42+
'services/development/app/index': {
43+
en: '/services/development/app',
44+
fr: '/offres/developement/app',
45+
},
46+
'services/development/website/index': {
47+
en: '/services/development/website',
48+
fr: '/offres/developement/site-web',
49+
},
50+
'services/coaching/index': {
51+
en: '/services/coaching',
52+
fr: '/offres/formation',
53+
},
54+
'random': {
55+
en: '/random',
56+
fr: false,
57+
},
58+
},
59+
},
60+
},
61+
})
62+
describe('i18n pages only locale', () => {
63+
it('basic', async () => {
64+
const index = await $fetch('/sitemap_index.xml')
65+
expect(index).toMatchInlineSnapshot(`
66+
"<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="/__sitemap__/style.xsl"?>
67+
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
68+
<sitemap>
69+
<loc>https://nuxtseo.com/__sitemap__/en-US.xml</loc>
70+
</sitemap>
71+
<sitemap>
72+
<loc>https://nuxtseo.com/__sitemap__/fr-FR.xml</loc>
73+
</sitemap>
74+
</sitemapindex>"
75+
`)
76+
const fr = await $fetch('/__sitemap__/fr-FR.xml')
77+
expect(fr).toMatchInlineSnapshot(`
78+
"<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="/__sitemap__/style.xsl"?>
79+
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
80+
<url>
81+
<loc>https://nuxtseo.com/fr/a-propos</loc>
82+
<xhtml:link rel="alternate" hreflang="en-US" href="https://nuxtseo.com/en/about" />
83+
<xhtml:link rel="alternate" hreflang="fr-FR" href="https://nuxtseo.com/fr/a-propos" />
84+
<xhtml:link rel="alternate" hreflang="x-default" href="https://nuxtseo.com/en/about" />
85+
</url>
86+
<url>
87+
<loc>https://nuxtseo.com/fr/offres</loc>
88+
<xhtml:link rel="alternate" hreflang="en-US" href="https://nuxtseo.com/en/services" />
89+
<xhtml:link rel="alternate" hreflang="fr-FR" href="https://nuxtseo.com/fr/offres" />
90+
<xhtml:link rel="alternate" hreflang="x-default" href="https://nuxtseo.com/en/services" />
91+
</url>
92+
<url>
93+
<loc>https://nuxtseo.com/fr/__sitemap/url</loc>
94+
<changefreq>weekly</changefreq>
95+
<xhtml:link rel="alternate" hreflang="x-default" href="https://nuxtseo.com/en/__sitemap/url" />
96+
<xhtml:link rel="alternate" hreflang="en-US" href="https://nuxtseo.com/en/__sitemap/url" />
97+
<xhtml:link rel="alternate" hreflang="fr-FR" href="https://nuxtseo.com/fr/__sitemap/url" />
98+
</url>
99+
<url>
100+
<loc>https://nuxtseo.com/fr/offres/developement</loc>
101+
<xhtml:link rel="alternate" hreflang="en-US" href="https://nuxtseo.com/en/services/development" />
102+
<xhtml:link rel="alternate" hreflang="fr-FR" href="https://nuxtseo.com/fr/offres/developement" />
103+
<xhtml:link rel="alternate" hreflang="x-default" href="https://nuxtseo.com/en/services/development" />
104+
</url>
105+
<url>
106+
<loc>https://nuxtseo.com/fr/offres/formation</loc>
107+
<xhtml:link rel="alternate" hreflang="en-US" href="https://nuxtseo.com/en/services/coaching" />
108+
<xhtml:link rel="alternate" hreflang="fr-FR" href="https://nuxtseo.com/fr/offres/formation" />
109+
<xhtml:link rel="alternate" hreflang="x-default" href="https://nuxtseo.com/en/services/coaching" />
110+
</url>
111+
<url>
112+
<loc>https://nuxtseo.com/fr/offres/developement/app</loc>
113+
<xhtml:link rel="alternate" hreflang="en-US" href="https://nuxtseo.com/en/services/development/app" />
114+
<xhtml:link rel="alternate" hreflang="fr-FR" href="https://nuxtseo.com/fr/offres/developement/app" />
115+
<xhtml:link rel="alternate" hreflang="x-default" href="https://nuxtseo.com/en/services/development/app" />
116+
</url>
117+
<url>
118+
<loc>https://nuxtseo.com/fr/offres/developement/site-web</loc>
119+
<xhtml:link rel="alternate" hreflang="en-US" href="https://nuxtseo.com/en/services/development/website" />
120+
<xhtml:link rel="alternate" hreflang="fr-FR" href="https://nuxtseo.com/fr/offres/developement/site-web" />
121+
<xhtml:link rel="alternate" hreflang="x-default" href="https://nuxtseo.com/en/services/development/website" />
122+
</url>
123+
</urlset>"
124+
`)
125+
}, 60000)
126+
})

0 commit comments

Comments
 (0)