Nuxt Tip: Rendering Modes
Michael Hoffmann
@mokkapps
Nuxt 3 offers three different rendering modes:
- Client-side rendering
- Universal rendering
- Hybrid rendering
Client-Side Rendering
Per default, a Vue.js application is rendered in the browser (or client). Vue.js generates HTML elements after the browser downloads and parses all the JavaScript code containing the instructions to create the current interface.
Read about the Pros & Cons.
Universal Rendering
By enabling universal (client-side + server-side) rendering, the server returns a fully rendered HTML page to the browser when the browser requests a URL.
In contrast to client-side rendering, users immediately get the application's content (similar to traditional server-side rendering).
To avoid losing the benefits of client-side rendering, the client loads the JavaScript code on the server in the background once the HTML document has been downloaded. The browser interprets it again, and Vue takes control of the document and enables interactivity. Hence it's called hence Universal rendering.
Universal rendering allows a Nuxt application to provide quick page load times while preserving the benefits of client-side rendering.
Read about the Pros & Cons.
Hybrid Rendering
Hybrid rendering allows different caching rules per route using route rules and decides how the server should respond to a new request on a given URL.
Route rules allow us to define rules for a group of Nuxt routes, change rendering mode or assign a cache strategy based on a route.
Let's take a look at an examplary configuration:
export default defineNuxtConfig({
routeRules: {
// Static page generated on-demand, revalidates in background
'/blog/**': { swr: true },
// Static page generated on-demand once
'/articles/**': { static: true },
// Set custom headers matching paths
'/_nuxt/**': { headers: { 'cache-control': 's-maxage=0' } },
// Render these routes with SPA
'/admin/**': { ssr: false },
// Add cors headers
'/api/v1/**': { cors: true },
// Add redirect headers
'/old-page': { redirect: '/new-page' },
'/old-page2': { redirect: { to: '/new-page', statusCode: 302 } },
},
})
You can define the following route rule options:
redirect
- Define server-side redirects.ssr
- Disables server-side rendering for sections of your app and make them SPA-only withssr: false
cors
- Automatically adds cors headers withcors: true
- you can customize the output by overriding with headersheaders
- Add specific headers to sections of your site - for example, your assetsstatic
- Enables a single (on-demand) buildswr
- Enables a static build that lasts for a configurable TTL (currently enables full incremental static generation on Netlify, with Vercel coming soon)
For more details, check the corresponding Nitro docs.
If you liked this Vue tip, follow me on BlueSky to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue & Nuxt newsletter :