From 9ee5ca2b4742b2662d85ba2ad3c795cd8937622f Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Wed, 14 Sep 2022 17:16:24 +0200 Subject: [PATCH] feat(nuxt): generate spa fallback for `nuxt generate` (#7507) --- packages/nuxt/package.json | 2 +- packages/nuxt/src/core/nitro.ts | 4 ++-- packages/nuxt/src/core/runtime/nitro/renderer.ts | 4 +++- yarn.lock | 12 ++++++------ 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/packages/nuxt/package.json b/packages/nuxt/package.json index b6e7ec2ad3..739d205303 100644 --- a/packages/nuxt/package.json +++ b/packages/nuxt/package.json @@ -58,7 +58,7 @@ "knitwork": "^0.1.2", "magic-string": "^0.26.3", "mlly": "^0.5.14", - "nitropack": "^0.5.2", + "nitropack": "^0.5.3", "nuxi": "3.0.0-rc.9", "ohash": "^0.1.5", "ohmyfetch": "^0.4.18", diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index 8b896e1d52..856158f2c7 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -56,8 +56,8 @@ export async function initNitro (nuxt: Nuxt) { prerender: { crawlLinks: nuxt.options._generate ? nuxt.options.generate.crawler : false, routes: ([] as string[]) - .concat(nuxt.options._generate ? ['/', ...nuxt.options.generate.routes] : []) - .concat(nuxt.options.ssr === false ? ['/', '/200.html', '/404.html'] : []) + .concat(nuxt.options._generate ? ['/', '/200.html', ...nuxt.options.generate.routes] : []) + .concat(nuxt.options.ssr === false ? ['/index.html', '/200.html', '/404.html'] : []) }, sourceMap: nuxt.options.sourcemap.server, externals: { diff --git a/packages/nuxt/src/core/runtime/nitro/renderer.ts b/packages/nuxt/src/core/runtime/nitro/renderer.ts index 66883ecba1..e909183793 100644 --- a/packages/nuxt/src/core/runtime/nitro/renderer.ts +++ b/packages/nuxt/src/core/runtime/nitro/renderer.ts @@ -106,6 +106,8 @@ const getSPARenderer = lazyCachedFunction(async () => { const PAYLOAD_CACHE = process.env.prerender ? new Map() : null // TODO: Use LRU cache const PAYLOAD_URL_RE = /\/_payload(\.[a-zA-Z0-9]+)?.js(\?.*)?$/ +const NO_SSR_ROUTES = new Set(['/index.html', '/200.html', '/404.html']) + export default defineRenderHandler(async (event) => { // Whether we're rendering an error page const ssrError = event.req.url?.startsWith('/__nuxt_error') @@ -130,7 +132,7 @@ export default defineRenderHandler(async (event) => { req: event.req, res: event.res, runtimeConfig: useRuntimeConfig() as NuxtSSRContext['runtimeConfig'], - noSSR: !!event.req.headers['x-nuxt-no-ssr'], + noSSR: !!(event.req.headers['x-nuxt-no-ssr']) || (process.env.prerender ? NO_SSR_ROUTES.has(url) : false), error: !!ssrError, nuxt: undefined!, /* NuxtApp */ payload: (ssrError ? { error: ssrError } : {}) as NuxtSSRContext['payload'] diff --git a/yarn.lock b/yarn.lock index 0b16846201..1573fc1fb2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10227,9 +10227,9 @@ __metadata: languageName: node linkType: hard -"nitropack@npm:^0.5.2": - version: 0.5.2 - resolution: "nitropack@npm:0.5.2" +"nitropack@npm:^0.5.3": + version: 0.5.3 + resolution: "nitropack@npm:0.5.3" dependencies: "@cloudflare/kv-asset-handler": ^0.2.0 "@netlify/functions": ^1.2.0 @@ -10243,7 +10243,7 @@ __metadata: "@rollup/pluginutils": ^4.2.1 "@vercel/nft": ^0.22.1 archiver: ^5.3.1 - c12: ^0.2.11 + c12: ^0.2.12 chalk: ^5.0.1 chokidar: ^3.5.3 consola: ^2.15.3 @@ -10292,7 +10292,7 @@ __metadata: bin: nitro: dist/cli.mjs nitropack: dist/cli.mjs - checksum: a27577af6902cbf606c56adcff0c2e88007c0eccfb31b674562fa36f88b91ddf02a2d490b08678aa6912e80ff8fc01502ff19cab4ba9cb488edf2223c89eee2f + checksum: f43ce535feb213b2a7f8c06e44befdb4058e4c6e5f87c5923e35256ab5fee03c9e71d3d7a3b4aa6bc707b371a203f962447ca5a0b1c10b69d0e5466e7cf6e5ee languageName: node linkType: hard @@ -10752,7 +10752,7 @@ __metadata: knitwork: ^0.1.2 magic-string: ^0.26.3 mlly: ^0.5.14 - nitropack: ^0.5.2 + nitropack: ^0.5.3 nuxi: 3.0.0-rc.9 ohash: ^0.1.5 ohmyfetch: ^0.4.18