CSR, SSR, SSG & ISR — Web Fundamentals

A rendering strategy answers where the HTML is made, when its data is read, and where a copy is kept. SSG prints (makes the HTML) once at build time, before…

Open a blog article and press Ctrl+U (Cmd+Option+U on a Mac) to see the raw HTML the server sent: the whole article is in there. Try the same on many dashboards (control pages behind a login), and often all you get is one empty <div> and a few <script> tags. Yet on screen both look complete.

The difference is the rendering strategy: when and where a page's HTML is made. The HTML can be made once at build time (when the site's code is prepared, before it is uploaded to the server), on every request, or in the reader's browser. The four strategies you meet most are SSG, SSR, CSR and ISR. Picture your page as the village paper, Village News, where printing means making the HTML. The paper reaches its readers in four ways, in the same order as those names:

You met the first three in the basics: printed in advance, printed to order, and filled in the browser.

Three questions for every page

Every strategy is an answer to three questions:

Your answers decide how fast the page arrives, how fresh its content is, how much the server costs, and whether search engines can read the content.

SSG: print once at build time

With SSG (Static Site Generation), your page's code runs once, at build time. The result is a plain .html file, and that file is spread across a CDN. Every reader gets the same file. This code shows the build step for the Village News page:

// build.js: runs ONCE, at deploy time
const news = [{ title: 'Clean-up day on Sunday morning' }, { title: 'Night market opens at 7' }]

function page(data) {
  const list = data.map((n) => `<li>${n.title}</li>`).join('')
  return `<h1>Village News</h1><ul>${list}</ul>`
}

const html = page(news) // saved as index.html, then spread to the CDN
console.log(html)

The result is <h1>Village News</h1><ul><li>Clean-up day on Sunday morning</li><li>Night market opens at 7</li></ul>. The content is already in the HTML, so search engines can read it. The page comes from the nearest CDN, so it is fast and almost free. The downside: the content only changes when you build again. Everyone also sees the same page, so an SSG page cannot hold personal data. It suits an About page, documentation and blog posts.

SSR: print on every request

With SSR (Server-Side Rendering), the server runs the page's code every time a request comes in, with the data of that second. In this code, the score changes between two requests:

let score = 'Oak Street 0, Jasmine Street 0'

function page(content) {
  return `<h1>Independence Day Football</h1><p>${content}</p>`
}

// SSR: the server runs this on EVERY request, with that second's data
function handle() {
  return page(score)
}

console.log(handle()) // a reader at 15:00
score = 'Oak Street 1, Jasmine Street 0'
console.log(handle()) // a reader at 15:10 gets the new score straight away

The reader at 15:00 sees a score of 0 to 0, and the reader at 15:10 right away sees Oak Street ahead by one goal. The content is always the latest, it is already in the HTML, and it can be made for each reader (the server can read their login cookie). The downside: every reader waits for their page to be printed, and the server works for every reader. If the content is personal, the reply must use Cache-Control: private or no-store, so the CDN does not keep it and give it to someone else.

CSR: the frame first, the browser fills it in

With CSR (Client-Side Rendering), the server or CDN only sends a frame (a page with no content) that is the same for everyone, plus JavaScript. The reader's browser runs that JavaScript, fetches the data with the reader's own login, and fills the page. This is the frame the CDN sends, with the script that fills it:

<!-- what the CDN sends: the same frame for everyone -->
<main id="app">Loading…</main>
<script>
  fetch('/api/my-subscription', { credentials: 'include' })
    .then((r) => r.json())
    .then((data) => {
      document.getElementById('app').textContent = 'Your subscription: ' + data.plan
    })
</script>

credentials: 'include' makes fetch send the reader's login cookie too. The frame may be kept on the CDN because it is the same for everyone, so it arrives fast. The data is fetched per reader, so everyone sees their own. The downside: the content only appears after the JavaScript has downloaded, run, and its data request has finished. A search engine that does not run JavaScript only sees "Loading…". It suits pages behind a login, such as dashboards and billing pages.

Hydration: bringing the HTML to life

HTML from SSG or SSR is already visible, but its buttons do nothing yet. To make them clickable, the page's JavaScript still has to download and run in the browser. Then the JavaScript attaches event handlers (functions that run when a button is clicked) to the HTML that is already there. This is called hydration. Here is an example for a Like button:

// app.js: run by the browser, attaching behaviour to a button already in the HTML
let likes = 12
const button = document.getElementById('like') // <button id="like">Like (12)</button>
button.addEventListener('click', () => {
  likes++
  button.textContent = `Like (${likes})`
})

Frameworks such as Next.js, Nuxt and SvelteKit do this for you: the same component code (the parts of the page) runs again in the browser. Until the JavaScript is done, the page looks complete but does not respond to clicks yet. So SSR does not mean "no JavaScript".

ISR: SSG that gets reprinted now and then

ISR (Incremental Static Regeneration) starts like SSG: the page is printed at build time and a copy is kept. The difference is that the copy has an age. In Next.js, an age of 60 seconds is written export const revalidate = 60.

Take a guess first. The Morning News page uses ISR with revalidate set to 60 seconds. It was printed at 09:00, the news changed at 09:30, and the next reader only arrives at 10:05. Do they get the old news or the new news? The answer is after the code below.

The rules:

  1. While the copy is younger than 60 seconds, every reader gets that copy.
  2. The first reader who arrives once the copy is 60 seconds old or more still gets the old copy, and their request starts a reprint in the background (the reader does not wait for it).
  3. The readers after them get the reprint.

This code follows those rules. Its seconds are counted from 09:00:

const REVALIDATE = 60 // seconds
let copy = { made: 0, content: 'news from 09:00' } // from the build

function request(second, newsNow) {
  const served = copy.content // a reader ALWAYS gets the copy that exists
  if (second - copy.made >= REVALIDATE) {
    // stale: this reader still gets the old one,
    // but their request starts a reprint in the background
    copy = { made: second, content: newsNow }
  }
  return served
}

console.log(request(30, 'news from 09:00'))   // 09:00:30
console.log(request(3900, 'news from 09:30')) // 10:05:00, the news changed at 09:30
console.log(request(3902, 'news from 09:30')) // 10:05:02

It prints news from 09:00, news from 09:00, then news from 09:30. So the reader at 10:05 gets the old news. Their request starts the reprint, but they do not wait for it. The reader two seconds later gets the new news. In the HTTP Caching lesson, a stale copy is revalidated (checked again with the server) before it is used. ISR is different: the stale copy is given out first, while it is reprinted in the background. In a Cache-Control header, this behaviour is called stale-while-revalidate.

So revalidate: 60 is not a promise that a copy is never older than 60 seconds. On a busy page, new news arrives about 60 seconds plus a few after it changes. On a quiet page that nobody opens for two days, the first reader afterwards gets a copy from two days ago. If the content must change as soon as it is edited, use on-demand revalidation. The CMS (the program you write the site's content in) then calls the server whenever something new is published, for example with revalidatePath('/news') in Next.js.

Comparing the four

Strategy HTML made Data read Content in the first HTML Server work per reader
SSG once, at build time at build time yes almost none, from the CDN
ISR at build time, then reprinted now and then at each reprint yes very little
SSR on every request, on the server on every request yes one print per reader
CSR in the reader's browser in the browser, after the JavaScript runs no, only the frame only the data request

Choosing per page

One site usually uses several strategies at once, one per page. Ask in order:

  1. Is the content different for each person? If so, do not keep the whole page on the CDN. A page behind a login that does not need to appear on Google: CSR, with the frame from the CDN. A public page that has personal parts and must be readable by search engines: SSR with Cache-Control: private.
  2. Is it the same for everyone and rarely changes? SSG.
  3. The same for everyone, changing a few times a day, with many readers? ISR, with a revalidate shorter than the oldest copy you can accept.
  4. Must it be the latest that very second, like a score during a match? SSR with no cache.

💡 TTFB, first paint, and clickable

Three numbers help compare the strategies:

There is also edge rendering: SSR run on a CDN server near the reader. It is only fast when the data is near too. If every request still has to ask a database on another continent, the time saved is spent on the data's trip.

⚠️ Why is the page stale, empty, or someone else's?

TL;DR: A rendering strategy answers where the HTML is made, when its data is read, and where a copy is kept. SSG prints (makes the HTML) once at build time, before the site is uploaded, and spreads it through a CDN: fast and cheap, but it only changes on the next build. SSR prints on every request: always the latest and can be personal, but every reader waits and the server keeps working. CSR sends the same frame (a page with no content) to everyone, then the browser fetches the data: right for pages behind a login, but the content is not in the first HTML. ISR keeps a copy like SSG, with an age (revalidate). The first reader after that age has passed still gets the old copy, and starts a reprint. A page with buttons still needs hydration: JavaScript that makes its buttons work when clicked.

⚡ SSG: print once at build time

Meaning: The page's code runs once at build time (when the site is prepared, before it is uploaded to the server). The result is a plain .html file spread to a CDN. Fast, almost free, and search engines can read the content. The downside: the content only changes when you build again, and everyone sees the same page. It suits an About page, documentation and blog posts.

⚡ SSR: print on every request

Meaning: The server runs the page's code on every request, with the data of that second. The content is always the latest and can be personal, but every reader waits for the page to be printed (made). Personal pages use Cache-Control: private, so the CDN does not keep them for other people. The buttons on an SSR page still wait for JavaScript to run (hydration), so SSR doesn't mean no JavaScript.

⚡ CSR: the frame first, the browser fills it in

Write:

<main id="app">Loading…</main>
<script>
  fetch('/api/my-subscription', { credentials: 'include' })
    .then((r) => r.json())
    .then((data) => {
      document.getElementById('app').textContent = 'Your subscription: ' + data.plan
    })
</script>
Guess first: what does a search engine that doesn't run JavaScript see on this page?

Only "Loading…". The content appears only after the JavaScript runs and the data arrives.

Meaning: The CDN sends the same frame (a page with no content) to everyone. Then JavaScript in the browser fetches the reader's own data, sending their login cookie (credentials: 'include'), and fills the page. It suits pages behind a login, such as dashboards and billing pages.

⚡ ISR: the stale copy still goes out first

Meaning: ISR keeps a copy like SSG, with an age, such as 60 seconds (revalidate = 60). The first reader who arrives once the copy is 60 seconds old or more still gets the old copy, and their request starts a reprint in the background (the reader does not wait for it). The readers after them get the new copy. So a quiet page nobody opened for two days can give out a copy from two days ago.

What you will learn