← All lessons
How server-side rendering works
Who builds the HTML
Browser
Server
Database
client.js
// index.html ships only <div id=root>import { render } from "./product.js";const res = await fetch("/api/product/42");const product = await res.json();root.innerHTML = render(product);server.js
// SSR: render on every requestapp.get("/product/:id", async (req, res) => { const product = await db.product(req.params.id); res.send(page(render(product)));}); // SSG: render once, at build timefor (const p of await db.products()) { write(`dist/product/${p.id}.html`, page(render(p)));}Race the three on a slow or fast network
first byte
content visible
interactive
HTML the browser receives
server.js
// CSRres.send('<div id="root"></div><script src="/app.js">');// SSRconst product = await db.product(42); // the database runs on every visitres.send(page(render(product)));// SSGcdn.serve("dist/product/42.html");Fresh or stale when the price changes after the build
page shows
real price
rendered at
product.js
// SSRapp.get("/product/42", async (req, res) => { res.send(render(await db.price(42)));});// SSGwrite("dist/product/42.html", render(await db.price(42)));// ISR: static, but re-render once it's 10 min oldres.set("Cache-Control", "s-maxage=600, stale-while-revalidate");Topics
Next: How hydration works
Open lesson