fix(nuxt): ensure static presets equivalent to nuxi generate (#21860)

This commit is contained in:
Daniel Roe 2023-06-29 10:14:35 +01:00 committed by GitHub
parent d4addcf7b1
commit 669e9bcf2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 19 deletions

View File

@ -29,7 +29,9 @@ export default defineNuxtCommand({
},
overrides: {
logLevel: args['log-level'],
// TODO: remove in 3.8
_generate: args.prerender,
...(args.prerender ? { nitro: { static: true } } : {}),
...(options?.overrides || {})
}
})

View File

@ -38,7 +38,6 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
}
const nitroConfig: NitroConfig = defu(_nitroConfig, {
static: nuxt.options._generate,
debug: nuxt.options.debug,
rootDir: nuxt.options.rootDir,
workspaceDir: nuxt.options.workspaceDir,
@ -133,10 +132,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
prerender: {
failOnError: true,
concurrency: cpus().length * 4 || 4,
crawlLinks: nuxt.options._generate ?? undefined,
routes: ([] as string[])
.concat(nuxt.options.generate.routes)
.concat(nuxt.options._generate ? [nuxt.options.ssr ? '/' : '/index.html', '/200.html', '/404.html'] : [])
routes: ([] as string[]).concat(nuxt.options.generate.routes)
},
sourceMap: nuxt.options.sourcemap.server,
externals: {
@ -360,6 +356,14 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/nitro.d.ts') })
})
if (nitro.options.static) {
nitro.hooks.hook('prerender:routes', (routes) => {
for (const route of [nuxt.options.ssr ? '/' : '/index.html', '/200.html', '/404.html']) {
routes.add(route)
}
})
}
// nuxt build/dev
nuxt.hook('build:done', async () => {
await nuxt.callHook('nitro:build:before', nitro)
@ -375,7 +379,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
await build(nitro)
logger.wrapAll()
if (nuxt.options._generate) {
if (nitro.options.static) {
const distDir = resolve(nuxt.options.rootDir, 'dist')
if (!existsSync(distDir)) {
await fsp.symlink(nitro.options.output.publicDir, distDir, 'junction').catch(() => {})

View File

@ -1,7 +1,7 @@
import { join, normalize, relative, resolve } from 'pathe'
import { createDebugger, createHooks } from 'hookable'
import type { LoadNuxtOptions } from '@nuxt/kit'
import { addBuildPlugin, addComponent, addPlugin, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolvePath, tryResolveModule } from '@nuxt/kit'
import { addBuildPlugin, addComponent, addPlugin, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit'
import type { Nuxt, NuxtHooks, NuxtOptions } from 'nuxt/schema'
import escapeRE from 'escape-string-regexp'
@ -271,15 +271,6 @@ async function initNuxt (nuxt: Nuxt) {
})
}
// Add prerender payload support
if (nuxt.options._generate && nuxt.options.experimental.payloadExtraction === undefined) {
console.warn('Using experimental payload extraction for full-static output. You can opt-out by setting `experimental.payloadExtraction` to `false`.')
nuxt.options.experimental.payloadExtraction = true
}
if (!nuxt.options.dev && nuxt.options.experimental.payloadExtraction) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/payload.client'))
}
// Add experimental cross-origin prefetch support using Speculation Rules API
if (nuxt.options.experimental.crossOriginPrefetch) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/cross-origin-prefetch.client'))
@ -381,6 +372,16 @@ async function initNuxt (nuxt: Nuxt) {
// Init nitro
await initNitro(nuxt)
// TODO: remove when app manifest support is landed in https://github.com/nuxt/nuxt/pull/21641
// Add prerender payload support
if (useNitro().options.static && nuxt.options.experimental.payloadExtraction === undefined) {
console.warn('Using experimental payload extraction for full-static output. You can opt-out by setting `experimental.payloadExtraction` to `false`.')
nuxt.options.experimental.payloadExtraction = true
}
if (!nuxt.options.dev && nuxt.options.experimental.payloadExtraction) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/payload.client'))
}
await nuxt.callHook('ready', nuxt)
}

View File

@ -201,8 +201,9 @@ export default defineNuxtModule({
})
})
// Prerender all non-dynamic page routes when generating app
if (!nuxt.options.dev && nuxt.options._generate) {
nuxt.hook('nitro:init', (nitro) => {
if (nuxt.options.dev || !nitro.options.static) { return }
// Prerender all non-dynamic page routes when generating app
const prerenderRoutes = new Set<string>()
nuxt.hook('modules:done', () => {
nuxt.hook('pages:extend', (pages) => {
@ -230,7 +231,7 @@ export default defineNuxtModule({
}
nitro.options.prerender.routes = Array.from(prerenderRoutes)
})
}
})
nuxt.hook('imports:extend', (imports) => {
imports.push(