Vue.js SSR and Nuxt
Server-side rendering, shortened to SSR, builds the HTML of a page on the server and sends finished content to the browser. A regular Vue application sends an almost empty HTML file and lets JavaScript build the page on the visitor's device. Search engines and social media previews may see an empty page, and visitors on slow phones wait for the script before seeing anything. SSR fixes both problems. Nuxt is the most popular framework that adds SSR to Vue with very little setup.
Think of a restaurant. A client-side app hands you a box of ingredients and a recipe. A server-side app serves the plated meal. The meal arrives ready to eat, and the browser only needs to add finishing touches.
Client-Side Rendering Compared with SSR
Diagram: Two Delivery Styles
Client-side rendering (CSR) Browser asks for page Server sends: <div id="app"></div> + JavaScript Browser runs JavaScript --> content appears Server-side rendering (SSR) Browser asks for page Server runs Vue, builds full HTML Server sends: complete page with text and images Browser shows content --> JavaScript attaches interactivity
The step where JavaScript attaches clicks and events to the delivered HTML is called hydration.
Benefits of SSR
- Search engines read complete content, which improves discoverability.
- Link previews on social platforms show titles and images.
- First content appears faster, especially on slow devices and networks.
Costs of SSR
- A server must run code, so static-only hosting is not enough.
- Code must avoid browser-only objects such as
windowduring the server step. - Each request consumes server resources.
Meet Nuxt
Nuxt wraps Vue with conventions that remove most boilerplate. It supplies file-based routing, automatic imports, data fetching helpers, SEO tools, and several rendering modes. A new project takes one command.
npx nuxi@latest init my-site
cd my-site
npm install
npm run devFile-Based Routing
Files inside the pages folder become routes automatically. No router configuration is needed.
pages/
index.vue --> /
about.vue --> /about
blog/
index.vue --> /blog
[slug].vue --> /blog/any-post-nameA file named with square brackets creates a dynamic route. Components read the value with useRoute().params.slug.
Automatic Imports and Layouts
Nuxt imports components, composables, and Vue functions such as ref automatically. A component saved as components/ProductCard.vue works in any page as <ProductCard /> with no import line. A file in the layouts folder wraps pages with shared headers and footers.
Fetch Data on the Server
The useFetch composable loads data during server rendering and passes the result to the browser, which avoids a second request.
<script setup>
const { data: posts, pending, error } = await useFetch("/api/posts");
</script>
<template>
<p v-if="pending">Loading...</p>
<p v-else-if="error">Posts could not load.</p>
<ul v-else>
<li v-for="p in posts" :key="p.id">{{ p.title }}</li>
</ul>
</template>Diagram: Data Flow in Nuxt
Request for /blog | Server runs useFetch --> gets posts | Server builds HTML with the posts included | Browser receives full page + saved data (no extra fetch)
Set Titles and Descriptions for SEO
<script setup>
useSeoMeta({
title: "Our Blog",
description: "Guides and news for new developers."
});
</script>Write Server API Routes
Nuxt includes a server folder for backend endpoints. A file at server/api/hello.js answers requests at /api/hello.
// server/api/hello.js
export default defineEventHandler(() => {
return { message: "Hello from the server" };
});Choose a Rendering Mode
Diagram: Rendering Choices
Universal (SSR) --> HTML built per request, best for changing content Static (SSG) --> HTML built once at build time, best for blogs and docs Client only (SPA) --> browser builds everything, best for private dashboards Hybrid --> mix of modes per route
Static generation runs npx nuxi generate and produces files that any static host can serve.
Avoid Browser-Only Code on the Server
The server has no window, document, or localStorage. Place browser code inside onMounted, which runs only in the browser, or wrap it in <ClientOnly>.
onMounted(() => {
const saved = localStorage.getItem("theme");
});Summary
SSR delivers complete HTML from the server, which helps search engines, link previews, and first-load speed. Hydration then makes the page interactive. Nuxt adds file-based routing, automatic imports, useFetch, SEO helpers, server API routes, and flexible rendering modes. Keep browser-only code inside onMounted and choose SSR, static generation, or client-only rendering based on the content.
