# Client-Side Routing

> How Single Page Applications (SPAs) change pages without ever reloading the browser, creating a fast, native-app experience.

Subject: React | Lesson 36 of 48 | 5 min read
Interactive lesson: https://codewithanimation.in/react/client-side-routing
Previous: [Concurrency in React 18](https://codewithanimation.in/react/concurrent-features.md) | Next: [React Router DOM](https://codewithanimation.in/react/react-router-dom.md)

## The Multi-Page App (MPA)

Historically, the web worked via Server-Side Routing. When you clicked a link to `/about`, your browser discarded the current page, sent a request to the server, and waited.

The server built a brand new HTML document and sent it back. The browser then parsed this new document, re-downloaded the CSS and JS, and painted the new page from scratch. This caused the 'white flash' of a full page reload.

## The Single Page App (SPA)

React applications are typically Single Page Applications. The server only ever sends *one* HTML document (`index.html`) containing an empty `<div id="root">`.

When a user clicks a link to `/about` in an SPA, React intercepts the click. It prevents the browser from sending a request to the server. Instead, it uses the browser's `History API` to instantly change the URL in the address bar.

React then looks at this new URL, unmounts the `<Home />` component, and mounts the `<About />` component. The page never reloads.

## The Mechanics

Under the hood, client-side routing relies on two browser features:

1. `window.history.pushState()`: Changes the URL in the address bar without triggering a page reload.

2. The `popstate` event: An event listener that fires when the user clicks the browser's Back or Forward buttons.

React routing libraries wrap these APIs in a declarative `<Router>` component that listens for URL changes and renders the matching `<Route>`.

## Common mistake: The SEO Tradeoff

Because an SPA only sends an empty `<div id="root">` from the server, web crawlers (like Googlebot) historically struggled to index them, as they had to execute the JavaScript to see the content.

This is why Meta-Frameworks like Next.js or Remix were created. They combine the best of both worlds: they render the initial HTML on the server (for SEO and fast initial load), but then 'hydrate' into an SPA, using client-side routing for all subsequent navigations.

## Tip: Preserving State

Because the page never reloads in an SPA, JavaScript memory is preserved across route changes. This means you can keep a music player playing in the global layout while the user navigates through different pages!

## Try it: The White Flash

Compare the navigation experience between an MPA (Server Routing) and an SPA (Client Routing). Watch what happens to the global audio player when you navigate between pages!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/client-side-routing

## Check yourself

### Question 1

What happens when you click a standard `<a>` tag with an `href` in a traditional Multi-Page Application (MPA)?

- A. The browser sends a request to the server, destroys the current DOM, and renders the new HTML document it receives.
- B. React intercepts the click and changes the state.
- C. The CSS files are re-applied to the existing DOM.

Correct answer: A

Traditional navigation causes a full page reload. The browser discards the current JavaScript memory and DOM, and starts over with the server's response.

- Why not B: That happens in an SPA, not an MPA.

### Question 2

How does Client-Side Routing change the URL without reloading the page?

- A. By using an invisible iframe.
- B. By utilizing the browser's HTML5 `History API` (specifically `pushState`).
- C. By asking the server for a special 304 code.

Correct answer: B

The `pushState` API allows JavaScript to update the URL displayed in the browser's address bar without telling the browser to actually navigate to that URL.

### Question 3

Which of the following is a major benefit of Client-Side Routing over Server-Side Routing?

- A. It is easier for search engines to index.
- B. It requires less JavaScript to be downloaded initially.
- C. Global state (like a playing audio track or a chat widget) can persist across page navigations.

Correct answer: C

Because a full page reload never occurs, the JavaScript environment remains intact. A component high up in the tree (like a layout) will not unmount when a child route changes.

- Why not A: Server-side routing is actually much better for SEO.

## Remember this

- Traditional MPAs cause a 'white flash' full-page reload on every navigation.
- React SPAs intercept clicks, prevent the default reload, and swap components dynamically.
- The HTML5 History API (`pushState` and `popstate`) makes this possible.
- Client-side routing feels instantaneous and preserves global state.
- Pure SPAs suffer in SEO, leading to the rise of SSR frameworks like Next.js.
