diff --git a/packages/nuxi/src/commands/build.ts b/packages/nuxi/src/commands/build.ts index 7cd50c3b3d..f4a9793b19 100644 --- a/packages/nuxi/src/commands/build.ts +++ b/packages/nuxi/src/commands/build.ts @@ -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 || {}) } }) diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index 36bfb0280c..d5ba2f2c4c 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -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(() => {}) diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index 12bc21c8a6..9f227cf8a1 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -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) } diff --git a/packages/nuxt/src/pages/module.ts b/packages/nuxt/src/pages/module.ts index 9744f7d7ef..726c3a0ffb 100644 --- a/packages/nuxt/src/pages/module.ts +++ b/packages/nuxt/src/pages/module.ts @@ -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() 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(