Skip to content
ClapelloNotes by Barath
ReactCh 07 · 2/6
Beginner4 min readUpdated

React / 35

Single-page apps & client-side routing

TL;DR

A multi-page app loads a new HTML page on every click. A single-page app keeps one page and swaps the content with JavaScript. How each works, and the trade-offs.

Multi-page app

server-side routing

click

Link to /menu

request

Browser asks the server for GET /menu

response

Server sends a complete new HTML page

reload

Old page is thrown away, the new one loads from scratch

Screen flashes. Anything in memory (form input, cart state) is gone.

Single-page app

client-side routing

click

Link to /menu

intercept

The router stops the browser's page load

url

It updates the address bar with history.pushState

render

React swaps in the <Menu /> component

No new page. Only JSON data, if needed, comes from the server. State survives.

Same click, two journeys. A multi-page app fetches a whole new document. A single-page app changes the URL and re-renders in place.

Multi-page apps: server-side routing

In a multi-page app (MPA), every URL is a separate request to the server, and the server sends back a complete HTML page. Deciding which page to send for which URL is server-side routing.

  • Good: each page is a real HTML document, so the first load is fast, search engines read it easily, and it works even if JavaScript fails.
  • Not so good: every click throws the current page away. Anything held in JavaScript memory (form input, open menus, app state) is lost, and the screen flashes.

Single-page apps: client-side routing

A single-page app (SPA) loads one HTML document, usually an almost empty index.html with a <div id="root"> and a script tag. From then on, JavaScript does the "page changes":

  1. Intercept the click

    The router stops the browser's normal page load.
  2. Change the URL

    It updates the address bar with the History API (history.pushState). No request is sent.
  3. Render the match

    React renders the component that belongs to the new URL.
  4. Fetch only data

    If the new screen needs data, it fetches JSON, not a whole page.

Here's the core of what a router does, in miniature:

tiny-router.js
document.addEventListener("click", (e) => {
  const link = e.target.closest("a");
  if (!link || link.origin !== location.origin) return; // external link: let it go
  e.preventDefault();                 // 1. stop the page load
  history.pushState({}, "", link.href); // 2. change the URL, no request
  render(location.pathname);          // 3. draw the matching screen
});
 
// Back / Forward buttons
window.addEventListener("popstate", () => render(location.pathname));

You won't write this yourself. React Router does it (plus nested layouts, URL params, errors and more), which is the rest of this chapter.

The trade-offs

Multi-page appSingle-page appSPA + server rendering
First loadFast, real HTMLBlank until JS loadsFast, real HTML
Clicking aroundFull reloadInstant, state keptInstant, state kept
Search enginesEasyHarder (content needs JS)Easy
Server setupRoutes on the serverServe index.html for every pathA Node (or similar) server
ExamplesClassic PHP sites, blogsVite + React RouterNext.js, React Router framework mode

The last column is how most production React apps work today. Server-side rendering (SSR) runs React on the server for the first request and sends real HTML. Then the JavaScript loads and hydrates the page (attaches event handlers to the existing HTML), and from there it navigates like an SPA. You get the fast first load and the instant clicks.

In development, Vite's dev server sends index.html for any unknown path, so refreshing /menu/7 just works. After deploying the built files to a static host, refreshing /menu/7 asks the server for a file called /menu/7, which doesn't exist, so you get a 404.

The fix is a rewrite rule on your host: "for any path that isn't a real file, send /index.html". Every static host (Netlify, Vercel, Cloudflare Pages, Nginx) has a setting for it. If you can't configure the server at all, a hash router avoids the problem. That's covered at the end of this chapter.

Gotchas

Recall

Can you answer these?

0/4

Answer out loud (or in your head) before you tap. That's what makes it stick.

Saved on this device only. No login.