Prerendering is powered by Nitro, Nuxt’s server engine, which provides crawl-based pre-rendering capabilities out of the box.
Crawl-Based Pre-rendering
Use thenuxt generate command to build and pre-render your application using the Nitro crawler. This command is similar to nuxt build with the nitro.static option set to true, or running nuxt build --prerender.
The crawler will build your site, stand up a Nuxt instance, and by default prerender the root page / along with any pages it links to, recursively following all discoverable links.
How the Nitro Crawler Works
The Nitro crawler follows this process:- Load HTML - Loads the HTML of your application’s root route (
/), any non-dynamic pages in your~/pagesdirectory, and any other routes in thenitro.prerender.routesarray - Save output - Saves the HTML and
payload.jsonto the~/.output/public/directory to be served statically - Find links - Finds all anchor tags (
<a href="...">) in the HTML to navigate to other routes - Repeat - Repeats steps 1-3 for each anchor tag found until there are no more anchor tags to crawl
Payload Extraction
Nuxt generates_payload.json files alongside HTML for:
- Prerendered routes (at build time)
- ISR/SWR routes (on first request)
useAsyncData and useFetch. During client-side navigation, these cached payloads are loaded instead of re-fetching data.
For dynamic routes like pages/[...slug].vue, configure route rules:
nuxt.config.ts
Selective Pre-rendering
You can manually specify routes that Nitro will fetch and pre-render during the build, or ignore routes that you don’t want to pre-render.Configuring Routes in nuxt.config
Specify routes to prerender and routes to ignore in yournuxt.config.ts file:
nuxt.config.ts
Combining with Crawl Links
Combine manual route specification with thecrawlLinks option to pre-render routes that the crawler can’t discover, like your /sitemap.xml or /robots.txt:
nuxt.config.ts
Setting
nitro.prerender to true is equivalent to setting nitro.prerender.crawlLinks to true.Using Route Rules
You can also configure prerendering usingrouteRules for fine-grained control:
nuxt.config.ts
Page-Level Configuration
As a shorthand, you can configure prerendering directly in a page file usingdefineRouteRules:
app/pages/index.vue
To use
defineRouteRules in page files, you must enable the experimental.inlineRouteRules option in your nuxt.config.nuxt.config.ts
Runtime Prerender Configuration
prerenderRoutes Function
You can useprerenderRoutes at runtime within a Nuxt context to add more routes for Nitro to prerender:
app/pages/index.vue
prerender:routes Nuxt Hook
This hook is called before prerendering to register additional routes:nuxt.config.ts
prerender:generate Nitro Hook
This hook is called for each route during prerendering, allowing fine-grained handling:nuxt.config.ts
Best Practices
When to use prerendering
When to use prerendering
Use prerendering for:
- Marketing pages and landing pages
- Blog posts and documentation
- E-commerce product pages
- Any content that doesn’t change frequently
When not to use prerendering
When not to use prerendering
Avoid prerendering for:
- User-specific dashboards
- Real-time data displays
- Pages with authentication requirements
- Content that changes frequently
Optimizing build times
Optimizing build times
For large sites:
- Use selective pre-rendering to limit the number of routes
- Consider using ISR (Incremental Static Regeneration) for frequently updated content
- Leverage the
ignoreoption to skip unnecessary routes