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

Commit

Permalink
feat(kit, nuxt): support prerender:routes and addPrerenderRoutes (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Nov 3, 2022
1 parent 9a67b9e commit ea17148
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
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

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

0 comments on commit ea17148

Please sign in to comment.