Client-Side Routing

Intermediate · 38 min

How single-page apps change the URL and swap views without a full page reload, and the browser API every router is built on.

Before thisComponent-Based Architecture

dcode · 38 min

Client-Side Routing

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by dcodeReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • By default, every URL change causes the browser to throw away the current page and fetch a brand new one from the server, which single-page apps need to avoid to feel instantaneous.
  • The History API's pushState lets JavaScript change the URL shown in the address bar without triggering a page reload, which is the foundation every client-side router is built on.
  • A router's job is to match the current URL against a list of route patterns and decide which component to render, including extracting dynamic segments like an id from the path.
  • Nested routing lets a URL segment map to a parent view that itself contains a sub-route, so layouts and deeply nested views can be composed the same way components are.
  • React Router, Vue Router, and every other framework router are all a layer of convenience on top of the same underlying browser mechanism — pushState and the popstate event.

The default behavior routing has to override

Normally, a URL change means a full round trip: the browser throws away everything currently in memory, sends a request to the server, and renders whatever HTML comes back from scratch. That’s fine for a traditional multi-page site. It’s a problem for an app that’s trying to feel instant — every navigation would mean a blank white flash, a re-download of your JavaScript bundle, and the loss of any in-memory state like a half-filled form or a scroll position.

Single-page apps exist specifically to avoid that. The whole app is loaded once, and from then on, “navigating” to a different page is handled entirely inside already-running JavaScript — no new document is requested from the server. Client-side routing is the piece that makes that feel like real navigation anyway: the URL in the address bar changes, the back button works, and bookmarking a page works, even though nothing was actually reloaded.

The mechanism underneath: the History API

The browser gives JavaScript direct control over this through the History API, and the method that matters most is history.pushState:

history.pushState({}, "", "/products/42");

This changes the URL shown in the address bar and adds a new entry to the browser’s session history — but it does not trigger a navigation or reload anything. The page stays exactly as it was. It’s genuinely just “update the address bar and remember this happened,” and everything else is left to your code.

That last part is the key insight: pushState on its own does nothing visible. It’s your application’s job to notice the URL changed and decide what to render in response. A minimal router is really just two things wired together: something that intercepts navigation (so a click on a link calls pushState instead of letting the browser do a real page load) and something that reads the current URL and renders the matching view.

There’s a second piece worth knowing: the popstate event. pushState doesn’t fire any event itself, which means it won’t tell your router something changed when the user clicks the browser’s back button — that’s a built-in history navigation, not a call your code made. popstate fires in exactly that situation, so a real router listens for both: it calls pushState on programmatic navigation, and it listens for popstate to catch the user moving through history with the browser’s own controls.

Matching routes: turning a URL into a decision

Once you can detect “the URL is now X,” the next job is deciding what to render for it. This is route matching, and it’s the same problem whether you’re using a tiny hand-rolled router or React Router: take the current path, compare it against a list of registered patterns, and find the one that fits.

/products          -> ProductList
/products/:id      -> ProductDetail
/products/:id/edit -> ProductEdit

The :id syntax is a dynamic segment — it matches any value in that position and makes it available to the matched component, so /products/42 matches the second pattern and hands 42 to ProductDetail as if it were a prop. This is the same “parameterized input” idea as function arguments or component props, just applied to URL structure instead of markup.

Nested routes: composing layouts the way you compose components

Real apps usually need more than a flat list of pages — a dashboard might have a persistent sidebar and header, with only the main content area changing as you navigate between /dashboard/overview and /dashboard/settings. Re-rendering the entire page, sidebar included, on every navigation would be wasteful and would lose things like scroll position in that sidebar.

Nested routing solves this by letting a route pattern match a parent component that itself contains a placeholder for a child route to render into — conceptually the same composition idea from components, just applied at the routing layer. /dashboard/* matches a DashboardLayout component with a sidebar and header, and whatever comes after /dashboard/ decides which component fills the content area inside that layout. The layout renders once; only the nested piece changes as the user moves between dashboard sub-pages.

Why every router looks different but does the same thing

React Router, Vue Router, Angular’s router, and dozens of smaller libraries all exist to save you from writing the plumbing above by hand — intercepting link clicks, matching patterns against the current path, extracting dynamic segments, re-rendering the right component tree, handling the back button correctly, scrolling to the top on navigation, and so on. The APIs look different: React Router gives you a <Route> component, Vue Router gives you a route config object, Angular gives you a routing module with its own syntax. Underneath every one of them is the same pair of browser primitives this lesson covered: pushState to change the URL without reloading, and popstate to notice when the user changed it themselves.

That’s worth internalizing before you ever open a specific router’s docs: you’re not learning a new concept each time, you’re learning a new interface to the same concept. The deeper-dive video on the History API is worth watching precisely because it strips routing down to that bare mechanism, with no framework’s abstractions in the way — a good gut check for whether you actually understand what a router does versus just how to call one.

Check yourself

  1. 01What does the browser normally do when the URL changes, and why is that a problem for a single-page app?

    It discards the current page and requests a fresh HTML document from the server, which would reset all in-memory state and cause a visible reload — exactly what a single-page app is trying to avoid.

  2. 02What does history.pushState actually do?

    It changes the URL shown in the address bar and adds an entry to the browser's history stack, without causing the browser to navigate away from or reload the current page.

  3. 03How does a router decide what to render for a given URL?

    It matches the current path against a list of registered route patterns, picks the one that fits (extracting any dynamic segments along the way), and renders the component associated with that match.

  4. 04Why is "popstate" necessary in addition to pushState?

    pushState doesn't fire when the user clicks the browser's back or forward button — popstate is the event that tells your code the URL changed that way, so the router can re-render the matching view.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark