React / 35
Single-page apps & client-side routing
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
/menurequest
GET /menuresponse
reload
Screen flashes. Anything in memory (form input, cart state) is gone.
Single-page app
client-side routing
click
/menuintercept
url
history.pushStaterender
<Menu /> componentNo new page. Only JSON data, if needed, comes from the server. State survives.
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":
Intercept the click
The router stops the browser's normal page load.Change the URL
It updates the address bar with the History API (history.pushState). No request is sent.Render the match
React renders the component that belongs to the new URL.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:
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 app | Single-page app | SPA + server rendering | |
|---|---|---|---|
| First load | Fast, real HTML | Blank until JS loads | Fast, real HTML |
| Clicking around | Full reload | Instant, state kept | Instant, state kept |
| Search engines | Easy | Harder (content needs JS) | Easy |
| Server setup | Routes on the server | Serve index.html for every path | A Node (or similar) server |
| Examples | Classic PHP sites, blogs | Vite + React Router | Next.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.
Deep links need a server fallback
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.
