Skip to content

Commit

Permalink
fix: ignore root when prerendering using strategy: 'prefix' (#2894)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede committed Apr 2, 2024
1 parent 78b6999 commit 62dfff5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/module.ts
Expand Up @@ -197,6 +197,25 @@ export default defineNuxtModule<NuxtI18nOptions>({
await setupPages(options, nuxt)
}

/**
* ignore `/` during prerender when using prefixed routing
*/

if (options.strategy === 'prefix' && nuxt.options._generate) {
const localizedEntryPages = normalizedLocales.map(x => ['/', x.code].join(''))
nuxt.hook('nitro:config', config => {
config.prerender ??= {}

// ignore `/` which is added by nitro by default
config.prerender.ignore ??= []
config.prerender.ignore.push(/^\/$/)

// add localized routes as entry pages for prerendering
config.prerender.routes ??= []
config.prerender.routes.push(...localizedEntryPages)
})
}

/**
* setup module alias
*/
Expand Down
7 changes: 7 additions & 0 deletions src/pages.ts
Expand Up @@ -57,6 +57,13 @@ export function setupPages(options: Required<NuxtI18nOptions>, nuxt: Nuxt) {
includeUnprefixedFallback,
optionsResolver: getRouteOptionsResolver(ctx, options)
})

// keep root when using prefixed routing without prerendering
const indexPage = pages.find(x => x.path === '/')
if (!nuxt.options._generate && options.strategy === 'prefix' && indexPage != null) {
localizedPages.unshift(indexPage)
}

pages.splice(0, pages.length)
pages.unshift(...(localizedPages as NuxtPage[]))
debug('... made pages', pages)
Expand Down

0 comments on commit 62dfff5

Please sign in to comment.