Server-side rendering basics
A client-rendered Vue app ships an empty page and fills it in after the JavaScript loads. On a slow connection the user stares at a blank screen, and a crawler may see nothing at all.
Server-side rendering (SSR) flips the order. The server runs your components, turns them into an HTML string, and sends that string with the page. The browser paints real content immediately, then Vue takes over.
Two pieces make it work. On the server you build the app with createSSRApp
instead of createApp, and render it with renderToString from
vue/server-renderer:
import { createSSRApp } from 'vue';
import { renderToString } from 'vue/server-renderer';
const app = createSSRApp(App);
const html = await renderToString(app);
The browser then hydrates that HTML: Vue attaches to the existing DOM instead
of rebuilding it. For hydration to work, the markup the server produced must
match what the client would render on its first pass. If they differ — a random
value, a Date.now(), a browser-only branch — Vue warns about a hydration
mismatch and repairs the tree, which costs the speed you were after.
SSR buys a faster first paint and indexable content. It costs a server that runs Vue, and the discipline to keep server and client output in step.
You are about to answer a short quiz on server-side rendering.