Vue3 Fundamentals ยท Nuxt & Server-Side Rendering

Server rendering and hydration

server rendering and hydration

Server rendering and hydration

A single-page app ships an empty <div> and builds the page in the browser, so the first paint waits on JavaScript. Nuxt renders your components to HTML on the server first, so the browser shows content immediately and search engines can read it.

Then the client takes over. Vue hydrates the server HTML: it walks the existing DOM, attaches event listeners, and makes the page interactive. It does not rebuild the DOM โ€” it adopts it.

Hydration only works if the HTML the client would render matches the HTML the server sent. When the two disagree, Vue reports a hydration mismatch and patches the difference, which is slower and can drop state.

Mismatches come from output that differs between the two runs. Date.now(), Math.random(), or reading localStorage during render produce one value on the server and another on the client:

<template>
  <p>{{ new Date().toLocaleTimeString() }}</p>
</template>

Keep render output deterministic. Move browser-only work into onMounted, which runs after hydration, or wrap the subtree in <ClientOnly> so the server skips it.

Server rendering gives you fast first paint and indexable HTML; hydration is the handover that makes that HTML live.

You are about to answer a short quiz on server rendering and hydration.

  1. Nuxt renders components to HTML on the server before the browser runs JavaScript.
  2. Hydration attaches event listeners to the server-rendered HTML instead of ___ it.
  3. A hydration mismatch happens when the client's rendered HTML differs from the server's.
  4. Reading `Math.random()` during render can cause a hydration ___.
  5. `onMounted` runs before hydration, on the server.
  6. Wrap browser-only markup in ___ so the server skips it.