Nuxt 09 ๐ Rendering Modes & Hybrid Rendering โ SSR, SSG, CSR, and routeRules
Nuxt is built on a rendering engine that allows for total flexibility. In older architectures, you had to choose a rendering mode for the entire application: either everything was rendered on the server, or everything was rendered in the browser. Nuxt breaks this binary. With its hybrid rendering architecture, you can define the rendering strategy on a per-route basis using the routeRules configuration. You can have a marketing landing page that is pre-rendered at build time, a dashboard that renders on-demand on the server, and a private user settings page that renders entirely in the client’s browser.
The engine behind this is Nitro, which intercepts incoming requests and applies the rendering strategy you defined for that specific URL. This chapter explores the spectrum of rendering modes available in Nuxt. We will cover Universal SSR (the default), Static Site Generation (SSG) via nuxt generate, Client-Side Rendering (CSR) by disabling SSR, and the powerful caching mechanisms known as SWR (Stale-While-Revalidate) and ISR (Incremental Static Regeneration).
Key point: Nuxtโs hybrid rendering strategy is defined via routeRules in nuxt.config.ts, allowing you to mix rendering modesโSSR, SSG, SWR, and SPAโwithin the same application, giving you the granularity to optimize each route for its specific performance and SEO requirements.
Why rendering modes exist
The build-time bottleneck. If you have 50,000 pages of content, building them all at deploy time (SSG) takes hours. You need a mode that builds only what is necessary or generates pages on-demand.
The server load problem. If every single request hits your API, your server will crash under high traffic. You need caching at the edge (SWR/ISR) to serve cached content without executing server-side logic for every user.
The hydration tax. A complex dashboard page doesn’t need SEO. Rendering it on the server, serializing the state, and then re-running the same JS in the browser (hydration) just wastes CPU cycles. You need a mode that bypasses SSR entirely (SPA/CSR).
The SEO conflict. Dynamic content that changes every second (like stock prices) cannot be static. Yet, you want the initial load to be fast for search engines. You need a mode that provides a cached version for the bot but fetches fresh data for the user.
The operational trade-off. Every rendering mode is a trade-off between build time, server cost, initial load speed, and runtime interactivity. Hybrid rendering exists because no single mode is correct for every page in a modern web application.
a. Universal SSR and the Default Mode
By default, Nuxt uses Universal Rendering. This means every request is handled by the server (or edge worker), which runs your setup functions, fetches data, renders the Vue components to HTML, and sends it to the browser. Once the browser receives the HTML, it “hydrates” the application, attaching event listeners and making it interactive.
// nuxt.config.ts
export default defineNuxtConfig({
ssr: true, // This is the default
});
This is the “everything” mode. It is great for SEO and initial load speed because the browser gets meaningful content immediately. However, it requires a Node.js server or an Edge function to execute at request time. If your content is static, this is overkill because you are re-generating the same HTML for every user request.
b. SSG and SPA Modes
Static Site Generation (SSG) happens when you run nuxt generate. Nuxt iterates over your routes and renders them into static HTML files at build time. There is no server involved at request time; a CDN or static file host serves the HTML, CSS, and JS.
# Generate the static site
npx nuxi generate
Client-Side Rendering (CSR/SPA) is the opposite of SSR. You disable SSR entirely, meaning the server sends a blank HTML shell, and the browser fetches the data and renders the components.
// nuxt.config.ts
export default defineNuxtConfig({
ssr: false, // Disables server-side rendering
});
You can also apply this per-route. If you have an admin dashboard that doesn’t need SEO, you can force it to be an SPA while keeping the rest of the site as SSR.
// nuxt.config.ts
export default defineNuxtConfig({
routeRules: {
'/admin/**': { ssr: false }, // Only this path is CSR
},
});
c. Hybrid Rendering with routeRules
The routeRules option is the crown jewel of Nuxt’s rendering architecture. It allows you to cache, redirect, and change rendering modes based on the route pattern. This is where SWR and ISR live.
SWR (Stale-While-Revalidate): The server renders the page, caches it, and serves it to users. When the cache expires, the server serves the stale version while generating a new version in the background.
// nuxt.config.ts
export default defineNuxtConfig({
routeRules: {
// Cache the blog index for 60 seconds
'/blog': { swr: 60 },
},
});
ISR (Incremental Static Regeneration): This is similar to SWR, but the cache never truly expires in the traditional sense; it regenerates on demand.
// nuxt.config.ts
export default defineNuxtConfig({
routeRules: {
// Regenerate the product page after 1 hour (3600 seconds)
'/products/**': { isr: 3600 },
},
});
This allows your application to be incredibly fast. Static pages are served instantly from the cache, and the server is only burdened with regeneration periodically, not on every single request.
Complete Example Session
// ============================================
// PART 1: GLOBAL CONFIGURATION
// ============================================
// nuxt.config.ts
export default defineNuxtConfig({
ssr: true, // Default
});
// ============================================
// PART 2: STATIC GENERATION (PRERENDERING)
// ============================================
// Prerender the home page at build time
export default defineNuxtConfig({
routeRules: {
'/': { prerender: true },
},
});
// ============================================
// PART 3: CLIENT-SIDE RENDERING (SPA)
// ============================================
// Force the dashboard to be CSR only
export default defineNuxtConfig({
routeRules: {
'/dashboard/**': { ssr: false },
},
});
// ============================================
// PART 4: CACHING WITH SWR
// ============================================
// Cache API response for 1 hour
export default defineNuxtConfig({
routeRules: {
'/api/data': { swr: 3600 },
},
});
// ============================================
// PART 5: INCREMENTAL STATIC REGENERATION
// ============================================
// Regenerate static pages periodically
export default defineNuxtConfig({
routeRules: {
'/blog/**': { isr: 3600 },
},
});
// ============================================
// PART 6: REDIRECTS
// ============================================
// Permanent redirect for old SEO links
export default defineNuxtConfig({
routeRules: {
'/old-path': { redirect: '/new-path' },
},
});
// ============================================
// PART 7: COMBINING RULES
// ============================================
// Complex hybrid setup
export default defineNuxtConfig({
routeRules: {
'/': { prerender: true },
'/api/**': { cors: true, headers: { 'cache-control': 'max-age=60' } },
'/admin/**': { ssr: false },
'/blog/**': { isr: 600 },
},
});
// ============================================
// PART 8: CACHE CONTROL HEADERS
// ============================================
// Manual header injection
export default defineNuxtConfig({
routeRules: {
'/static-assets/**': { headers: { 'cache-control': 'public, max-age=31536000' } },
},
});
// ============================================
// PART 9: CORS PREFLIGHT
// ============================================
// Allow CORS for API routes
export default defineNuxtConfig({
routeRules: {
'/api/v1/**': { cors: true },
},
});
// ============================================
// PART 10: DISABLING SSR FOR A SPECIFIC COMPONENT
// ============================================
// Inside a page component
<template>
<ClientOnly>
<ExpensiveDashboardComponent />
</ClientOnly>
</template>
The example session demonstrated the syntax for routeRules, ranging from basic static generation and CSR enforcement to advanced caching strategies like SWR and ISR, as well as utility rules like redirects and CORS.
Quick Reference
Rendering Mode Options
| Rule | Description |
|---|---|
ssr: true | Universal rendering (default) |
ssr: false | Client-side only (SPA) |
prerender: true | Generated at build time |
swr: <seconds> | Cache, then serve stale/revalidate |
isr: <seconds> | Incremental Static Regeneration |
Utility Rules
| Rule | Description |
|---|---|
redirect | Permanent or temporary redirect |
cors | Enable CORS headers |
headers | Inject custom response headers |
cache | Control browser/CDN caching |
Performance Comparison
| Mode | Build Time | Server Load | First Paint |
|---|---|---|---|
| SSG | High | None | Instant |
| SSR | Low | High | Medium |
| SWR/ISR | Low | Low | Instant/Medium |
| SPA | Low | None | Slow |
Best Practices
โ Do This:
// Prerender the home page for speed
'/': { prerender: true }, // โ
// Use ISR for heavy but infrequent content
'/blog/**': { isr: 3600 }, // โ
// Use SPA for authenticated dashboards
'/dashboard/**': { ssr: false }, // โ
// Set reasonable TTLs for cache
'/api/feed': { swr: 300 }, // โ
// Redirect old routes to preserve SEO
'/old': { redirect: '/new' }, // โ
โ Don’t Do This:
// Prerender pages that depend on the request
'/user-profile': { prerender: true }, // โ (will be identical for all)
// Set SWR to 0 seconds
'/api': { swr: 0 }, // โ
// Use SPA for SEO-critical pages
'/marketing-page': { ssr: false }, // โ
// Overuse ISR for rapidly changing data
'/stock-ticker': { isr: 60 }, // โ
// Forget to handle dynamic params in ISR
'/products/**': { isr: 3600 }, // โ (ensure patterns match)
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Hydration mismatch | SSR and Client render different HTML | onMounted or ClientOnly |
| Build time hangs | Prerendering too many routes | Use nitro.prerender.crawlLinks |
| Stale data forever | Cache TTL too high | Lower TTL or use swr |
| Blank page in SPA | JS failed to load | Ensure correct ssr: false usage |
| 404s on dynamic routes | Route not pre-generated | Configure nitro.prerender.routes |
Real-World Examples
1. Blog posts with ISR
routeRules: { '/posts/**': { isr: 3600 } }
2. Marketing landing page (SSG)
routeRules: { '/landing': { prerender: true } }
3. User dashboard (SPA)
routeRules: { '/app/**': { ssr: false } }
4. API endpoint caching
routeRules: { '/api/weather': { swr: 60 } }
5. Redirecting old URLs
routeRules: { '/docs/v1/**': { redirect: '/docs/v2/**' } }
6. CORS for external clients
routeRules: { '/api/public/**': { cors: true } }
7. Setting security headers
routeRules: { '/**': { headers: { 'X-Frame-Options': 'DENY' } } }
8. Prerendering specific dynamic routes
nitro: { prerender: { routes: ['/product/1', '/product/2'] } }
9. Disabling SSR for a plugin-heavy page
routeRules: { '/map-view': { ssr: false } }
10. Forcing SSR for a specific route
routeRules: { '/ssr-only': { ssr: true } }
Visual
Rendering Pipeline
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ RENDERING PATHS โ
โ โ
โ SSR: User โโโบ Server โโโบ HTML โโโบ Browser โ
โ โ
โ SSG: Build โโโบ Static File โโโบ Browser โ
โ โ
โ SPA: User โโโบ Blank HTML โโโบ JS โโโบ Render โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
ISR/SWR Mechanism
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ CACHING LOGIC โ
โ โ
โ 1. Request โโโบ Cache Hit? โโโบ Yes: Return โ
โ 2. Cache Miss? โโโบ Render โโโบ Save Cache โ
โ 3. Stale? โโโบ Return Stale โโโบ Revalidate โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
routeRules Decision Tree
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ CONFIGURING routeRules โ
โ โ
โ Is it SEO critical? โ
โ โโ No โโโบ ssr: false (SPA) โ
โ โโ Yes โโโบ Is it static content? โ
โ โโ Yes โโโบ prerender: true โ
โ โโ No โโโบ Needs caching? โ
โ โโ Yes โโโบ swr/isr โ
โ โโ No โโโบ SSR โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| Universal Rendering | ssr: true |
| Build-time | prerender: true |
| Client-only | ssr: false |
| Stale-While-Revalidate | swr: <seconds> |
| Incremental Static | isr: <seconds> |
| Configuration | nuxt.config.ts |
| Granularity | Path pattern matching |
Key takeaways:
- Nuxt rendering is hybrid. You do not need to choose one strategy for the entire site; you can define the strategy per route using
routeRulesinnuxt.config.ts. - SSR is the default. Universal SSR provides the best SEO and initial load time but requires server execution at request time.
- SSG is best for performance. Prerendering (
prerender: true) generates static files at build time, eliminating server cost for those routes. - SPA mode is for dashboards. Disabling SSR (
ssr: false) is ideal for authenticated, private areas where SEO is irrelevant and component interactivity is high. - SWR and ISR scale your site. These rules cache content at the edge, serving static-like speeds for dynamic content while handling revalidation in the background.
- Route matching is powerful. Use pattern matching (e.g.,
/blog/**) to apply rules to entire sections of your application in one line.
Remember: The power of Nuxt lies in its ability to adapt to the specific needs of each page in your application. By utilizing routeRules, you can combine the SEO benefits of SSR, the speed of SSG, and the interactivity of SPA, ensuring that your application is optimized for both the search engine bot and the end user, without the operational burden of managing separate infrastructures for each rendering mode.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!