Rendering Modes
Nuxt supports various rendering modes.
Specifically, these include Universal Rendering, Client-Side Rendering, and Hybrid Rendering.
By default, Universal Rendering is selected.
These modes can easily be switched via nuxt.config
.
Universal Rendering
In Universal Rendering, the server returns fully rendered HTML.
This allows the user to access the application's content immediately.
After loading the HTML, the browser also loads JavaScript code to build dynamic UI that requires interaction.
This process is called "hydration".
In this example, we log a count
state to the console in app.vue
.
When accessing the page, you can see the output both in the terminal (server) and in the browser console.
This means that Nuxt executes Vue.js code on the server to generate HTML, and then runs the same code again in the browser.
This is why it is called "Universal Rendering".
The main advantages and disadvantages of Universal Rendering are as follows:
Advantages
- Performance
Since HTML is generated on the server and read by the browser, it is faster than generating content using JavaScript in the browser. - Search Engine Optimization (SEO)
Web crawlers can directly index the page content, which is beneficial for SEO.
Disadvantages
- Development constraints
Writing code that runs seamlessly on both server and client sides imposes some limitations. - Cost
Requires a server, which incurs operational costs.
For more details, see the official documentation.
Client-Side Rendering
You can enable Client-Side Rendering by setting ssr: false
in nuxt.config
.
// nuxt.config.ts
export default defineNuxtConfig({
ssr: false
})
With Client-Side Rendering, the application is rendered in the browser.
The browser downloads and parses all JavaScript code, which contains the instructions to construct the UI, then generates HTML elements.
The main advantages and disadvantages of Client-Side Rendering are as follows:
Advantages
- Development speed
You only need to consider how the application works in the browser, without worrying about server integration. - Cost-efficient
No server is needed, so infrastructure costs are lower. - Offline capability
Since all code runs in the browser, the app can continue working even without an internet connection.
Disadvantages
- Performance
Users have to wait for the browser to download, parse, and execute JavaScript, which may affect user experience. - Search Engine Optimization (SEO)
Indexing and updating content rendered on the client side takes time, which is less SEO-friendly compared to server-rendered HTML.
Hybrid Rendering
Nuxt allows you to specify different cache rules and rendering modes for each route using route rules.
This is configured via the routeRules
option in nuxt.config
.
For more details, refer to the official documentation.
export default defineNuxtConfig({
routeRules: {
// Homepage pre-rendered at build time
'/': { prerender: true },
// Blog list page generated on demand, revalidates in background, cached on CDN for 1 hour (3600 seconds)
'/blog': { isr: 3600 },
// Blog post pages generated on demand once until next deployment, cached on CDN
'/blog/**': { isr: true },
// Admin dashboard rendered only on the client-side
'/admin/**': { ssr: false },
}
})
Challenge
Set up Client-Side Rendering and verify that Vue.js code runs only in the browser.
To do this:
- Set
ssr: false
innuxt.config
. - Log a
count
state to the console inapp.vue
. - Confirm that there is no output in the terminal.
- Open the browser devtools and verify that the console shows the output.
Next, set up Hybrid Rendering and confirm that you can assign different cache rules and rendering modes per route.
To do this:
- Use the
NuxtPage
component inapp.vue
to render pages. - Add console output in the
script setup
of/pages/index.vue
and/pages/foo.vue
(any message is fine). - Set
routeRules
innuxt.config
, applying different cache and rendering settings to/
and/foo
.
This time, setssr: false
for/foo
. - Visit
/
and confirm that the console output appears both on the server and in the browser. - Visit
/foo
and confirm that the console output appears only in the browser.
To verify rendering behavior, it's important to check not just console output, but also the request behavior using the browser's "Network" tab.
With Universal Rendering, since HTML is generated on the server, you'll see a fully populated <div id="__nuxt">
in the network response.
With Client-Side Rendering, the HTML will initially be empty and populated by JavaScript.
(The JavaScript will be visible as separate download requests)