Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(prerendering): dynamic pages using modules #300

Merged
merged 2 commits into from
Sep 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/content/1.docs/3.recipes/3.pre-rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,29 @@ export default defineNuxtConfig({
})
```

### Using a Nuxt Module

You can also use a [local Nuxt module](https://nuxt.com/docs/guide/going-further/modules) to pre-render dynamic pages, which is particularly useful if you don't have a single root page (such as `/blog`) but still need to pre-render specific routes, such as `/page-1`, `/parent/page-2`, and so on.

```ts [modules/prerender-routes.ts]
import { defineNuxtModule, addPrerenderRoutes } from '@nuxt/kit'

export default defineNuxtModule({
meta: {
name: 'nuxt-prerender-routes',
},
async setup() {
const pages = await getDynamicPages()
addPrerenderRoutes(pages)
},
})

async function getDynamicPages(): string[] {
// Replace this function with the logic for retrieving the slugs for your pages.
return ['/page-1', '/parent/page-2']
}
atinux marked this conversation as resolved.
Show resolved Hide resolved
```

## Pre-render All Pages

To have the same behavior as [`nuxt generate`](https://nuxt.com/docs/api/commands/generate) while keeping the server part, you can pre-render all pages by configuring the `nitro.prerender` option in the `nuxt.config.ts`:
Expand Down