Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

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

Merged
merged 1 commit into from Nov 3, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/3.api/4.advanced/1.hooks.md
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions docs/content/3.api/4.advanced/2.kit.md
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions packages/kit/src/nitro.ts
Expand Up @@ -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
*
Expand Down
3 changes: 3 additions & 0 deletions packages/nuxt/src/core/nitro.ts
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions packages/schema/src/types/hooks.ts
Expand Up @@ -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
pi0 marked this conversation as resolved.
Show resolved Hide resolved

// Nuxi
'build:error': (error: Error) => HookResult
Expand Down