@@ -11,7 +11,7 @@ import {
11
11
hasNuxtModuleCompatibility ,
12
12
useLogger ,
13
13
} from '@nuxt/kit'
14
- import { withBase , withoutLeadingSlash } from 'ufo'
14
+ import { withBase , withLeadingSlash , withoutLeadingSlash , withoutTrailingSlash } from 'ufo'
15
15
import { installNuxtSiteConfig } from 'nuxt-site-config-kit'
16
16
import type { NuxtI18nOptions } from '@nuxtjs/i18n'
17
17
import { defu } from 'defu'
@@ -27,7 +27,7 @@ import type {
27
27
SitemapSourceBase ,
28
28
SitemapSourceInput ,
29
29
SitemapSourceResolved ,
30
- ModuleOptions as _ModuleOptions ,
30
+ ModuleOptions as _ModuleOptions , FilterInput ,
31
31
} from './runtime/types'
32
32
import { convertNuxtPagesToSitemapEntries , generateExtraRoutesFromNuxtConfig , resolveUrls } from './util/nuxtSitemap'
33
33
import { createNitroPromise , createPagesPromise , extendTypes , getNuxtModuleOptions , resolveNitroPreset } from './util/kit'
@@ -151,13 +151,14 @@ export default defineNuxtModule<ModuleOptions>({
151
151
let resolvedAutoI18n : false | AutoI18nConfig = typeof config . autoI18n === 'boolean' ? false : config . autoI18n || false
152
152
const hasDisabledAutoI18n = typeof config . autoI18n === 'boolean' && ! config . autoI18n
153
153
let normalisedLocales : NormalisedLocales = [ ]
154
+ let usingI18nPages = false
154
155
if ( hasNuxtModule ( '@nuxtjs/i18n' ) ) {
155
156
const i18nVersion = await getNuxtModuleVersion ( '@nuxtjs/i18n' )
156
157
if ( ! await hasNuxtModuleCompatibility ( '@nuxtjs/i18n' , '>=8' ) )
157
158
logger . warn ( `You are using @nuxtjs/i18n v${ i18nVersion } . For the best compatibility, please upgrade to @nuxtjs/i18n v8.0.0 or higher.` )
158
159
nuxtI18nConfig = ( await getNuxtModuleOptions ( '@nuxtjs/i18n' ) || { } ) as NuxtI18nOptions
159
160
normalisedLocales = mergeOnKey ( ( nuxtI18nConfig . locales || [ ] ) . map ( ( locale : any ) => typeof locale === 'string' ? { code : locale } : locale ) , 'code' )
160
- const usingI18nPages = Object . keys ( nuxtI18nConfig . pages || { } ) . length
161
+ usingI18nPages = ! ! Object . keys ( nuxtI18nConfig . pages || { } ) . length
161
162
if ( usingI18nPages && ! hasDisabledAutoI18n ) {
162
163
const i18nPagesSources : SitemapSourceBase = {
163
164
context : {
@@ -448,6 +449,35 @@ declare module 'vue-router' {
448
449
449
450
// for each sitemap, we need to transform the include and exclude
450
451
// if the include or exclude has a URL without a locale prefix, then we insert all locale prefixes
452
+ if ( resolvedAutoI18n && usingI18nPages && ! hasDisabledAutoI18n ) {
453
+ const pages = nuxtI18nConfig ?. pages || { } as Record < string , Record < string , string > >
454
+ for ( const sitemapName in sitemaps ) {
455
+ if ( [ 'index' , 'chunks' ] . includes ( sitemapName ) )
456
+ continue
457
+ const sitemap = sitemaps [ sitemapName ]
458
+ function mapToI18nPages ( path : FilterInput ) : FilterInput [ ] {
459
+ if ( typeof path !== 'string' )
460
+ return [ path ]
461
+ const withoutSlashes = withoutTrailingSlash ( withoutLeadingSlash ( path ) ) . replace ( '/index' , '' )
462
+ if ( withoutSlashes in pages ) {
463
+ const pageLocales = pages [ withoutSlashes ]
464
+ // @ts -expect-error untyped
465
+ return Object . keys ( pageLocales ) . map ( localeCode => withLeadingSlash ( generatePathForI18nPages ( { localeCode, pageLocales : pageLocales [ localeCode ] , nuxtI18nConfig, normalisedLocales } ) ) )
466
+ }
467
+ let match = [ path ]
468
+ // alternatively see if the path matches the default locale within
469
+ Object . values ( pages ) . forEach ( ( pageLocales ) => {
470
+ // @ts -expect-error untyped
471
+ if ( nuxtI18nConfig . defaultLocale in pageLocales && pageLocales [ nuxtI18nConfig . defaultLocale ] === path )
472
+ // @ts -expect-error untyped
473
+ match = Object . keys ( pageLocales ) . map ( localeCode => withLeadingSlash ( generatePathForI18nPages ( { localeCode, pageLocales : pageLocales [ localeCode ] , nuxtI18nConfig, normalisedLocales } ) ) )
474
+ } )
475
+ return match
476
+ }
477
+ sitemap . include = ( sitemap . include || [ ] ) . flatMap ( path => mapToI18nPages ( path ) )
478
+ sitemap . exclude = ( sitemap . exclude || [ ] ) . flatMap ( path => mapToI18nPages ( path ) )
479
+ }
480
+ }
451
481
if ( resolvedAutoI18n && resolvedAutoI18n . locales && resolvedAutoI18n . strategy !== 'no_prefix' ) {
452
482
const i18n = resolvedAutoI18n
453
483
for ( const sitemapName in sitemaps ) {
0 commit comments