fix(nuxt): extend nitro routes instead of overriding (#5828)

This commit is contained in:
pooya parsa 2022-07-12 12:56:40 +02:00 committed by GitHub
parent 1840b96335
commit 56a3f1b615
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -54,29 +54,27 @@ export default defineNuxtModule({
// Prerender all non-dynamic page routes when generating app // Prerender all non-dynamic page routes when generating app
if (!nuxt.options.dev && nuxt.options._generate) { if (!nuxt.options.dev && nuxt.options._generate) {
const routes = new Set<string>() const prerenderRoutes = new Set<string>()
nuxt.hook('modules:done', () => { nuxt.hook('modules:done', () => {
nuxt.hook('pages:extend', (pages) => { nuxt.hook('pages:extend', (pages) => {
routes.clear() prerenderRoutes.clear()
for (const path of nuxt.options.nitro.prerender?.routes || []) {
routes.add(path)
}
const processPages = (pages: NuxtPage[], currentPath = '/') => { const processPages = (pages: NuxtPage[], currentPath = '/') => {
for (const page of pages) { for (const page of pages) {
// Skip dynamic paths // Skip dynamic paths
if (page.path.includes(':')) { continue } if (page.path.includes(':')) { continue }
const route = joinURL(currentPath, page.path)
const path = joinURL(currentPath, page.path) prerenderRoutes.add(route)
routes.add(path) if (page.children) { processPages(page.children, route) }
if (page.children) { processPages(page.children, path) }
} }
} }
processPages(pages) processPages(pages)
}) })
}) })
nuxt.hook('nitro:build:before', (nitro) => { nuxt.hook('nitro:build:before', (nitro) => {
nitro.options.prerender.routes = [...routes] for (const route of nitro.options.prerender.routes || []) {
prerenderRoutes.add(route)
}
nitro.options.prerender.routes = Array.from(prerenderRoutes)
}) })
} }