fix(nuxt): seed crawler when prerendering pages (#27955)

Co-authored-by: Josh Dean <jdbdean@gmail.com>
This commit is contained in:
Daniel Roe 2024-07-01 22:57:43 +01:00 committed by GitHub
parent f78dfed22e
commit 9f4f4d562e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 59 additions and 5 deletions

View File

@ -503,11 +503,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
for (const route of ['/200.html', '/404.html']) { for (const route of ['/200.html', '/404.html']) {
routes.add(route) routes.add(route)
} }
if (nuxt.options.ssr) { if (!nuxt.options.ssr) {
if (nitro.options.prerender.crawlLinks) {
routes.add('/')
}
} else {
routes.add('/index.html') routes.add('/index.html')
} }
}) })

View File

@ -3,6 +3,7 @@ import { mkdir, readFile } from 'node:fs/promises'
import { addBuildPlugin, addComponent, addPlugin, addTemplate, addTypeTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, findPath, logger, resolvePath, updateTemplates, useNitro } from '@nuxt/kit' import { addBuildPlugin, addComponent, addPlugin, addTemplate, addTypeTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, findPath, logger, resolvePath, updateTemplates, useNitro } from '@nuxt/kit'
import { dirname, join, relative, resolve } from 'pathe' import { dirname, join, relative, resolve } from 'pathe'
import { genImport, genObjectFromRawEntries, genString } from 'knitwork' import { genImport, genObjectFromRawEntries, genString } from 'knitwork'
import { joinURL } from 'ufo'
import type { Nuxt, NuxtApp, NuxtPage } from 'nuxt/schema' import type { Nuxt, NuxtApp, NuxtPage } from 'nuxt/schema'
import { createRoutesContext } from 'unplugin-vue-router' import { createRoutesContext } from 'unplugin-vue-router'
import { resolveOptions } from 'unplugin-vue-router/options' import { resolveOptions } from 'unplugin-vue-router/options'
@ -18,6 +19,8 @@ import type { PageMetaPluginOptions } from './plugins/page-meta'
import { PageMetaPlugin } from './plugins/page-meta' import { PageMetaPlugin } from './plugins/page-meta'
import { RouteInjectionPlugin } from './plugins/route-injection' import { RouteInjectionPlugin } from './plugins/route-injection'
const OPTIONAL_PARAM_RE = /^\/?:.*(?:\?|\(\.\*\)\*)$/
export default defineNuxtModule({ export default defineNuxtModule({
meta: { meta: {
name: 'pages', name: 'pages',
@ -269,6 +272,61 @@ export default defineNuxtModule({
} }
}) })
// Record all pages for use in prerendering
const prerenderRoutes = new Set<string>()
function processPages (pages: NuxtPage[], currentPath = '/') {
for (const page of pages) {
// Add root of optional dynamic paths and catchalls
if (OPTIONAL_PARAM_RE.test(page.path) && !page.children?.length) {
prerenderRoutes.add(currentPath)
}
// Skip dynamic paths
if (page.path.includes(':')) { continue }
const route = joinURL(currentPath, page.path)
prerenderRoutes.add(route)
if (page.children) {
processPages(page.children, route)
}
}
}
nuxt.hook('pages:extend', (pages) => {
if (nuxt.options.dev) { return }
prerenderRoutes.clear()
processPages(pages)
})
// For static sites with ssr: false with crawl, prerender all routes
nuxt.hook('nitro:init', (nitro) => {
if (nuxt.options.dev || !nitro.options.static || nuxt.options.router.options.hashMode || !nitro.options.prerender.crawlLinks) { return }
// Only hint the first route when `ssr: true` and no routes are provided
if (nuxt.options.ssr) {
nitro.hooks.hook('prerender:routes', (routes) => {
if ([...routes].every(r => r.endsWith('.html'))) {
const [firstPage] = [...prerenderRoutes].sort()
if (firstPage) {
routes.add(firstPage)
}
}
})
return
}
// Prerender all non-dynamic page routes when generating `ssr: false` app
nuxt.hook('nitro:build:before', (nitro) => {
for (const route of nitro.options.prerender.routes || []) {
prerenderRoutes.add(route)
}
nitro.options.prerender.routes = Array.from(prerenderRoutes)
})
})
nuxt.hook('imports:extend', (imports) => { nuxt.hook('imports:extend', (imports) => {
imports.push( imports.push(
{ name: 'definePageMeta', as: 'definePageMeta', from: resolve(runtimeDir, 'composables') }, { name: 'definePageMeta', as: 'definePageMeta', from: resolve(runtimeDir, 'composables') },