Tech Interview RoomStudy guides

Vue.js

What is Nuxt and how does server-side rendering work in Vue?

Advanced · 2 min read · Lesson 12 of 12 in Vue.js

Nuxt is the full-stack framework for Vue (as Next.js is for React). Nuxt 3/4 is built on Vue 3, Vite and the Nitro server engine.

What it adds

  • File-based routing from pages/, layouts, and middleware for guards.
  • Rendering modes per route with routeRules: SSR, static pre-rendering (SSG), client-only (SPA), and cached/revalidated rendering (similar to ISR).
  • Data fetching with useFetch and useAsyncData: runs on the server during SSR, sends the result to the client in the payload, and does not fetch again during hydration.
  • Server routes in server/api/ (an API backend in the same project).
  • Auto-imports of components, composables and Vue APIs.
  • SEO helpers (useHead, useSeoMeta), modules for images, fonts, content, i18n.

How SSR works

  1. The server runs the app for the requested URL, waits for data, and renders HTML with renderToString (or a stream).
  2. The browser shows the HTML immediately (fast first paint, crawlable content).
  3. The client bundle loads and hydrates: Vue builds the same virtual DOM, attaches to the existing DOM and adds event listeners.

SSR pitfalls

  • Hydration mismatches: render output differing between server and client (dates, random values, window checks). Use <ClientOnly>, onMounted, or stable values.
  • Cross-request state pollution: module-level reactive state is shared by all users on the server. Use useState in Nuxt or Pinia, which create state per request.
  • Browser APIs are not available in setup on the server.

Example

vue
<!-- pages/products/[id].vue -->
<script setup lang="ts">
const route = useRoute();
const { data: product, error } = await useFetch(`/api/products/${route.params.id}`);
if (error.value) throw createError({ statusCode: 404, statusMessage: 'Product not found' });

useSeoMeta({ title: () => product.value?.name, description: () => product.value?.summary });
</script>

<template>
  <h1>{{ product?.name }}</h1>
  <ClientOnly><RecentlyViewed /></ClientOnly>
</template>
ts
// server/api/products/[id].get.ts
export default defineEventHandler(async event => {
  const id = getRouterParam(event, 'id');
  return await db.products.findById(id);
});

// nuxt.config.ts
export default defineNuxtConfig({
  routeRules: {
    '/': { prerender: true },
    '/products/**': { swr: 600 },      // cached for 10 minutes, refreshed in the background
    '/dashboard/**': { ssr: false },
  },
});

Follow-up questions interviewers ask

Try answering each one before you open it.

SSR vs SSG vs SPA: how do you choose?

SSG (pre-render at build) for content that rarely changes: marketing pages, docs, blogs; fastest and cheapest. SSR for personalised or frequently changing pages that need SEO and fast first paint, such as product pages. SPA for authenticated dashboards where SEO does not matter. Nuxt and Next.js let you mix them per route.