feat(kit, nuxt): support prerender:routes and addPrerenderRoutes (#8670)

This commit is contained in:
pooya parsa 2022-11-03 22:03:12 +01:00 committed by GitHub
parent 9a67b9e4f3
commit ea17148a6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 1 deletions

View File

@ -31,7 +31,7 @@ Hook | Arguments | Environment | Description
# Nuxt Hooks (build time)
Check the [schema source code](https://github.com/nuxt/framework/blob/main/packages/schema/src/types/hooks.ts#L69) for all available hooks.
Check the [schema source code](https://github.com/nuxt/framework/blob/main/packages/schema/src/types/hooks.ts#L53) for all available hooks.
:NeedContribution

View File

@ -77,6 +77,8 @@ description: Nuxt Kit provides composable utilities to help interacting with Nux
- `addServerHandler (handler)`
- `addDevServerHandler (handler)`
- `useNitro()` (only usable after `ready` hook)
- `addServerPlugin`
- `addPrerenderRoutes`
### Resolving

View File

@ -41,6 +41,25 @@ export function addServerPlugin (plugin: string) {
nuxt.options.nitro.plugins.push(normalize(plugin))
}
/**
* Adds routes to be prerendered
*/
export function addPrerenderRoutes (routes: string | string[]) {
const nuxt = useNuxt()
if (!Array.isArray(routes)) {
routes = [routes]
}
routes = routes.filter(Boolean)
if (!routes.length) {
return
}
nuxt.hook('prerender:routes', (ctx) => {
for (const route of routes) {
ctx.routes.add(route)
}
})
}
/**
* Access to the Nitro instance
*

View File

@ -148,6 +148,9 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
// Connect hooks
nuxt.hook('close', () => nitro.hooks.callHook('close'))
nitro.hooks.hook('prerender:routes', (routes) => {
nuxt.callHook('prerender:routes', { routes })
})
// Setup handlers
const devMiddlewareHandler = dynamicEventHandler()

View File

@ -96,6 +96,7 @@ export interface NuxtHooks {
'nitro:config': (nitroConfig: NitroConfig) => HookResult
'nitro:init': (nitro: Nitro) => HookResult
'nitro:build:before': (nitro: Nitro) => HookResult
'prerender:routes': (ctx: { routes: Set<string> }) => HookResult
// Nuxi
'build:error': (error: Error) => HookResult