Skip to content

Commit 62dfff5

Browse files
authored
fix: ignore root when prerendering using strategy: 'prefix' (#2894)
1 parent 78b6999 commit 62dfff5

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/module.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,25 @@ export default defineNuxtModule<NuxtI18nOptions>({
197197
await setupPages(options, nuxt)
198198
}
199199

200+
/**
201+
* ignore `/` during prerender when using prefixed routing
202+
*/
203+
204+
if (options.strategy === 'prefix' && nuxt.options._generate) {
205+
const localizedEntryPages = normalizedLocales.map(x => ['/', x.code].join(''))
206+
nuxt.hook('nitro:config', config => {
207+
config.prerender ??= {}
208+
209+
// ignore `/` which is added by nitro by default
210+
config.prerender.ignore ??= []
211+
config.prerender.ignore.push(/^\/$/)
212+
213+
// add localized routes as entry pages for prerendering
214+
config.prerender.routes ??= []
215+
config.prerender.routes.push(...localizedEntryPages)
216+
})
217+
}
218+
200219
/**
201220
* setup module alias
202221
*/

src/pages.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ export function setupPages(options: Required<NuxtI18nOptions>, nuxt: Nuxt) {
5757
includeUnprefixedFallback,
5858
optionsResolver: getRouteOptionsResolver(ctx, options)
5959
})
60+
61+
// keep root when using prefixed routing without prerendering
62+
const indexPage = pages.find(x => x.path === '/')
63+
if (!nuxt.options._generate && options.strategy === 'prefix' && indexPage != null) {
64+
localizedPages.unshift(indexPage)
65+
}
66+
6067
pages.splice(0, pages.length)
6168
pages.unshift(...(localizedPages as NuxtPage[]))
6269
debug('... made pages', pages)

0 commit comments

Comments
 (0)