Rendering: SSR, SSG, ISR, streaming
Every page's HTML has to be made at some point: at build time, on every request, or refreshed in the background. Choosing SSG, SSR, ISR or streaming (and PPR in Next.js 16) decides how fast and how fresh your pages are.
62.1 When is the HTML made?
- SSG (Static Site Generation): HTML made once at build time; every user gets the same ready page (e.g. the about page).
- SSR (Server Side Rendering): HTML made on the server on every request (e.g. the cart, different per user).
- ISR (Incremental Static Regeneration): page stays static but is refreshed in the background after a fixed time.
- CSR (Client Side Rendering): a client component fetches data in the browser.
| Strategy | When HTML is made | Example | Next.js 16 tool |
|---|---|---|---|
| SSG | Once at build | About page, blog | "use cache" |
| ISR | At build + timed background refresh | Product price | "use cache" + cacheLife |
| SSR / streaming | On every request | Cart, cookies, search params | <Suspense> |
| CSR | In the browser | Live widgets | Client Component fetch |
62.2 Problem: the build fails
- The product page is the same for every user, so we want it static: generateStaticParams builds ids 1 and 2.
- getProduct reads from the database, and npm run build fails with "uncached or runtime data during prerendering".
// app/product/[id]/page.tsximport { getProduct } from "@/lib/products"; export async function generateStaticParams() { return [{ id: "1" }, { id: "2" }];} export default async function Page({ params,}: PageProps<"/product/[id]">) { const { id } = await params; const p = await getProduct(id); return <h1>{p.name}: ₹{p.price}</h1>;}$ npm run buildError: Route "/product/[id]": Next.js encountereduncached or runtime data during prerendering.Ways to fix this: - [stream] Provide a placeholder with `<Suspense fallback={...}>` around the data access - [cache] For uncached data (`fetch`, database calls): cache the access with `"use cache"`⨯ Next.js build worker exited with code: 162.3 Why did the build fail?
- The default create-next-app setup in Next.js 16 uses Cache Components: every page is rendered in advance at build (prerendering).
- Next does not guess whether a DB result is the same for everyone or must be fresh per request; you decide.
- Same data for everyone -> "use cache". Different per request -> wrap it in Suspense.
- Step 1
1.npm run build prerenders every page
- Step 2
2.It finds a DB call / fetch / cookies read
- Step 3
3.Is the result the same for everyone?
- Step 4
4.Yes -> "use cache" (static, SSG / ISR)
- Step 5
5.No, per request -> <Suspense> (streamed at request time)
- Step 6
6.Neither -> build fails with uncached data error
62.4 Fix: "use cache" = SSG
- Write the "use cache" directive on the first line of getProduct; Next caches the function's result.
- The build passes: the DB query runs only twice (product 1 and 2) and both become static pages (○).
- After npm start, open the page any number of times and no query goes to the database.
// lib/products.tsimport { db } from "@/lib/db"; export async function getProduct(id: string) { "use cache"; return db.product.find(id);}$ npm run buildDB query: product 1DB query: product 2Route (app)└ /product/[id] ├ ◐ /product/[id] ├ ○ /product/1 └ ○ /product/2 $ npm start▲ Next.js 16.4.5✓ Ready in 709ms62.5 cacheLife = ISR
- Prices change; a page static forever would show the old price after the sale ends.
- cacheLife("hours") says the data is fresh for one hour, then refreshes in the background.
- The build output now shows Revalidate 1h and Expire 1d next to the product pages.
import { cacheLife } from "next/cache";import { db } from "@/lib/db"; export async function getProduct(id: string) { "use cache"; cacheLife("hours"); return db.product.find(id);}$ npm run buildRoute (app) Revalidate Expire├ /product/[id]│ ├ ◐ /product/[id]│ ├ ○ /product/1 1h 1d│ └ ○ /product/2 1h 1d- Step 1
1.Build: page is made
- Step 2
2.For 1 hour: every user instantly gets the cached page
- Step 3
3.Hour over: first visitor still gets the old page, regeneration starts in background
- Step 4
4.Next visitors get the new page with the updated price
Nobody waits for the regeneration
62.6 Per user data: stream with Suspense
- The cart differs per user (read from cookies), so it cannot be cached.
- Wrap CartItems in Suspense with a Loading fallback: heading and Loading show immediately, cart items stream in later.
// app/cart/page.tsximport { Suspense } from "react";import { CartItems } from "./cart-items"; export default function Cart() { return ( <main> <h1>Your Cart</h1> <Suspense fallback={<p>Loading...</p>}> <CartItems /> </Suspense> </main> );}# Your CartLoading... (a moment later)# Your Cart2 items in cart62.7 PPR: static shell + dynamic hole
- Partial Prerendering: at build, the heading and the Suspense fallback become a static shell served from cache instantly.
- On a request, the server runs CartItems and its HTML streams in the same response, replacing Loading.
- One page has static and dynamic parts; the build output marks it with the half circle (◐) Partial Prerender symbol.
- Step 1
1.Build: shell = heading + fallback
- Step 2
2.Request arrives: shell served immediately from cache
- Step 3
3.Server runs CartItems for this request
- Step 4
4.Its HTML streams in the same response and replaces Loading
62.8 Checkpoint: what shows first?
- Heading, Loading and Free delivery are all in the static shell, so they show immediately.
- Only the Stock line (live, per request) arrives two seconds later.
import { Suspense } from "react";import { Stock } from "./stock"; export default function Page() { return ( <main> <h1>iPhone 17</h1> <Suspense fallback={<p>Loading...</p>}> <Stock /> </Suspense> <p>Free delivery</p> </main> );}# iPhone 17Loading...Free delivery (2 seconds later)# iPhone 17Only 3 left in stockFree delivery62.9 Common mistakes
- Wrapping the whole page in one big Suspense because cookies are read somewhere inside: the shell has only Loading and even cached product data waits for the request.
- Fix: keep Suspense as low as possible so ProductInfo is in the shell and only CartCount streams.
- Myth: Server Component = SSR. Wrong: Server vs Client is WHERE code runs; SSR/SSG/ISR is WHEN HTML is made. A Server Component can be static, and a Client Component is also rendered to HTML on the server.
// Wrong: shell has only "Loading..."export default function Page() { return ( <Suspense fallback={<p>Loading...</p>}> <ProductPage /> </Suspense> );}// Right: product in the shell, only the cart streamsexport default function Page() { return ( <main> <ProductInfo /> <Suspense fallback={<p>Cart: ...</p>}> <CartCount /> </Suspense> </main> );}62.10 When to use which
- Same for everyone (about page, blog): static + "use cache".
- Changes sometimes (price): ISR with cacheLife.
- Different per user or request (cart, cookies, search params): Suspense.
- Always keep Suspense as low as possible.
62.11 Interview tips
- SSG: HTML at build time. SSR: on every request. ISR: static page regenerated in the background after a set time.
- In Next.js 16 with Cache Components, uncached data outside Suspense fails the build: choose "use cache" or Suspense.
- cacheLife turns a static page into ISR.
- PPR is the default: static shell first, dynamic parts stream in with Suspense.
- Separate WHERE (Server/Client Component) from WHEN (SSG/SSR/ISR).