Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/nuxi/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {})
}
})
Expand Down
16 changes: 10 additions & 6 deletions packages/nuxt/src/core/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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)
Expand All @@ -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(() => {})
Expand Down
21 changes: 11 additions & 10 deletions packages/nuxt/src/core/nuxt.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 4 additions & 3 deletions packages/nuxt/src/pages/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -230,7 231,7 @@ export default defineNuxtModule({
}
nitro.options.prerender.routes = Array.from(prerenderRoutes)
})
}
})

nuxt.hook('imports:extend', (imports) => {
imports.push(
Expand Down