Full answer
In a traditional multi-page application (MPA) each link asks the server for a new HTML document and the browser replaces the whole page. A single-page application (SPA) loads a shell once. When the user clicks a link, JavaScript intercepts the click, changes the URL without a reload and renders the matching component.
How it works in React. React itself has no router. A library such as React Router listens for link clicks, calls history.pushState to change the URL, listens for the back and forward buttons (popstate), and renders the component for the current URL. The example below builds a tiny version with the hashchange event. Real routers use the History API for clean URLs, so the server must return the same index.html for every path, or deep links return a 404.
Benefits of an SPA: fast transitions after the first load, state kept across pages (a playing video, an open sidebar), smooth animations between views and a single codebase for an app-like experience.
Costs: a larger first download (all the framework code), a blank page until JavaScript runs (hurts first paint on slow devices), weaker out-of-the-box search engine optimization and link previews because the initial HTML is empty, extra work for accessibility (focus and announcements after navigation, scroll restoration, page titles), and memory growth in long sessions.
Mitigations: code splitting by route, server-side rendering or static generation (Next.js, Remix and similar frameworks), prerendering key pages and a loading skeleton.
When an MPA is better: mostly content pages (blogs, documentation, marketing). Modern frameworks blur the line by serving HTML for the first request and then taking over as an SPA.
The example is a minimal hash router that changes content without loading a new page.
import { useSyncExternalStore } from 'react';
const subscribe = (callback) => {
window.addEventListener('hashchange', callback);
return () => window.removeEventListener('hashchange', callback);
};
const getPath = () => window.location.hash.slice(1) || '/';
const pages = {
'/': 'Home',
'/about': 'About',
'/courses': 'Courses',
};
export default function App() {
const path = useSyncExternalStore(subscribe, getPath);
return (
<div>
<nav>
<a href="#/">Home</a> <a href="#/about">About</a> <a href="#/courses">Courses</a>
</nav>
<h1>{pages[path] ?? 'Not found'}</h1>
</div>
);
}The check clicked About and then Courses. The content changed and the number of page loads stayed at zero, because only the hash changed. A hash router is the simplest form. Real routers use the History API. Output of the run: start: Home | url hash | page loads 0 after clicking About: About | url hash #/about | page loads 0 after clicking Courses: Courses | url hash #/courses | page loads 0
Key points
- An SPA loads one page and swaps content with JavaScript while changing the URL with the History API.
- React needs a router library for URLs, and the server must serve index.html for all paths.
- Benefits: fast navigation and preserved state. Costs: heavy first load, SEO and accessibility work.
- Mitigate with route code splitting, server rendering or static generation.
- Content-heavy sites often fit multi-page or server-rendered approaches better.
Common mistakes
- Forgetting that the server must return index.html for every route when using the History API.
- Ignoring page titles, focus and scroll position after a client-side navigation.
- Using plain anchors with onClick and breaking open-in-new-tab behaviour.
- Shipping the whole app in one bundle.
- Relying on an SPA for content that needs good search indexing, without server rendering.
Follow-ups
- How does React Router change the URL without a reload?
- How would server-side rendering change the first load of an SPA?
- How do you handle focus for screen readers after a client-side navigation?
Tips
- Mention the History API and the index.html fallback.
- State both the benefits and the SEO and accessibility costs.
- Say modern frameworks combine server rendering with SPA navigation.