mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-31 07:40:33 +00:00
feat(nuxt): prerender all pages by default (#5709)
This commit is contained in:
parent
5e419e75c4
commit
271262e10a
@ -3,7 +3,8 @@ import { defineNuxtModule, addTemplate, addPlugin, addVitePlugin, addWebpackPlug
|
||||
import { resolve } from 'pathe'
|
||||
import { genString, genImport, genObjectFromRawEntries } from 'knitwork'
|
||||
import escapeRE from 'escape-string-regexp'
|
||||
import { NuxtApp } from '@nuxt/schema'
|
||||
import type { NuxtApp, NuxtPage } from '@nuxt/schema'
|
||||
import { joinURL } from 'ufo'
|
||||
import { distDir } from '../dirs'
|
||||
import { resolvePagesRoutes, normalizeRoutes } from './utils'
|
||||
import { TransformMacroPlugin, TransformMacroPluginOptions } from './macros'
|
||||
@ -51,6 +52,34 @@ export default defineNuxtModule({
|
||||
}
|
||||
})
|
||||
|
||||
// Prerender all non-dynamic page routes when generating app
|
||||
if (!nuxt.options.dev && nuxt.options._generate) {
|
||||
const routes = new Set<string>()
|
||||
nuxt.hook('modules:done', () => {
|
||||
nuxt.hook('pages:extend', (pages) => {
|
||||
routes.clear()
|
||||
for (const path of nuxt.options.nitro.prerender?.routes || []) {
|
||||
routes.add(path)
|
||||
}
|
||||
const processPages = (pages: NuxtPage[], currentPath = '/') => {
|
||||
for (const page of pages) {
|
||||
// Skip dynamic paths
|
||||
if (page.path.includes(':')) { continue }
|
||||
|
||||
const path = joinURL(currentPath, page.path)
|
||||
routes.add(path)
|
||||
if (page.children) { processPages(page.children, path) }
|
||||
}
|
||||
}
|
||||
processPages(pages)
|
||||
})
|
||||
})
|
||||
|
||||
nuxt.hook('nitro:build:before', (nitro) => {
|
||||
nitro.options.prerender.routes = [...routes]
|
||||
})
|
||||
}
|
||||
|
||||
nuxt.hook('autoImports:extend', (autoImports) => {
|
||||
autoImports.push({ name: 'definePageMeta', as: 'definePageMeta', from: resolve(runtimeDir, 'composables') })
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user