From c0edd1dce5a449817f2c074fe26d7b4ecf6a6e53 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 4 Jul 2022 11:05:27 +0100 Subject: [PATCH 1/3] feat(nuxt): prerender all pages by default --- packages/nuxt/src/pages/module.ts | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/nuxt/src/pages/module.ts b/packages/nuxt/src/pages/module.ts index fcbec4e5067..1618c510736 100644 --- a/packages/nuxt/src/pages/module.ts +++ b/packages/nuxt/src/pages/module.ts @@ -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' @@ -51,6 +52,31 @@ export default defineNuxtModule({ } }) + // Add all pages to be prerendered + const routes = new Set() + + nuxt.hook('pages:extend', (pages) => { + routes.clear() + for (const path of nuxt.options.nitro.prerender?.routes || []) { + routes.add(path) + } + function 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') }) }) From 6bc8b018ae836aa7be484625445c296673a85f04 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 4 Jul 2022 11:33:24 +0100 Subject: [PATCH 2/3] fix: only prerender all pages when generating --- packages/nuxt/src/pages/module.ts | 42 ++++++++++++++++--------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/packages/nuxt/src/pages/module.ts b/packages/nuxt/src/pages/module.ts index 1618c510736..6cdb43a2e23 100644 --- a/packages/nuxt/src/pages/module.ts +++ b/packages/nuxt/src/pages/module.ts @@ -52,30 +52,32 @@ export default defineNuxtModule({ } }) - // Add all pages to be prerendered - const routes = new Set() + // Prerender all non-dynamic page routes when generating app + if (!nuxt.options.dev && nuxt.options._generate) { + const routes = new Set() - nuxt.hook('pages:extend', (pages) => { - routes.clear() - for (const path of nuxt.options.nitro.prerender?.routes || []) { - routes.add(path) - } - function processPages (pages: NuxtPage[], currentPath = '/') { - for (const page of pages) { - // Skip dynamic paths - if (page.path.includes(':')) { continue } - - const path = joinURL(currentPath, page.path) + nuxt.hook('pages:extend', (pages) => { + routes.clear() + for (const path of nuxt.options.nitro.prerender?.routes || []) { routes.add(path) - if (page.children) { processPages(page.children, path) } } - } - processPages(pages) - }) + 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('nitro:build:before', (nitro) => { + nitro.options.prerender.routes = [...routes] + }) + } nuxt.hook('autoImports:extend', (autoImports) => { autoImports.push({ name: 'definePageMeta', as: 'definePageMeta', from: resolve(runtimeDir, 'composables') }) From 05733d88a7810c59a6fd9faf06bd6e5cc25c0622 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Thu, 7 Jul 2022 11:50:10 +0100 Subject: [PATCH 3/3] fix: register `pages:extend` last --- packages/nuxt/src/pages/module.ts | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/nuxt/src/pages/module.ts b/packages/nuxt/src/pages/module.ts index 6cdb43a2e23..c5f35f90007 100644 --- a/packages/nuxt/src/pages/module.ts +++ b/packages/nuxt/src/pages/module.ts @@ -55,23 +55,24 @@ export default defineNuxtModule({ // Prerender all non-dynamic page routes when generating app if (!nuxt.options.dev && nuxt.options._generate) { const routes = new Set() - - 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) + nuxt.hook('modules:done', () => { + nuxt.hook('pages:extend', (pages) => { + routes.clear() + for (const path of nuxt.options.nitro.prerender?.routes || []) { routes.add(path) - if (page.children) { processPages(page.children, path) } } - } - processPages(pages) + 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) => {