Skip to content

Commit

Permalink
feat(nuxt): prerender all pages by default (nuxt#5709)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jul 7, 2022
1 parent 5e419e7 commit 271262e
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion packages/nuxt/src/pages/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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') })
})
Expand Down

0 comments on commit 271262e

Please sign in to comment.