Sub topic 1
How Client-Side Routing Works in Angular
Prerequisites
This chapter assumes you already know:
- TypeScript fundamentals, including classes, modules, and arrow functions.
- Angular components and templates.
- Standalone components and the
inject()function. - Dependency injection and services in Angular.
- Basic RxJS Observables. The router reports much of its state through them.
Server Routing vs Client-Side Routing
In a traditional multi-page site, every link click sends a request to the server. The server returns a new HTML document, and the browser throws away the current page and renders the new one. Any state held in memory, such as form input or scroll position, is lost.
A single-page application (SPA) loads one HTML document at the start. After that, JavaScript decides what to show. When the URL changes, the application swaps components in and out of the page. It does not ask the server for a new document. Client-side routing is the mapping from URLs to views that the application performs in the browser.
| Aspect | Server routing | Client-side routing |
|---|---|---|
| Who handles a URL change | The server | JavaScript in the browser |
| Full page reload | Yes | No |
| In-memory state | Lost | Kept |
| Deep links and bookmarks | Work by default | The server must return `index.html` for app URLs |
The Browser API Underneath
The router depends on the History API. history.pushState() changes the address bar without a reload, and the popstate event fires when the user presses Back or Forward. Here is a minimal hand-built router in plain TypeScript that shows the idea:
const views: Record<string, string> = {
'/home': '<h2>Home</h2>',
'/about': '<h2>About</h2>',
};
function render(path: string): void {
const outlet = document.getElementById('outlet')!;
outlet.innerHTML = views[path] ?? '<h2>Not found</h2>';
}
function navigate(path: string): void {
history.pushState({}, '', path);
render(path);
}
window.addEventListener('popstate', () => render(location.pathname));
document.addEventListener('click', (event) => {
const link = (event.target as HTMLElement).closest('a[data-link]');
if (link) {
event.preventDefault();
navigate(link.getAttribute('href')!);
}
});
render(location.pathname);This toy version leaves out a lot: parameters like /products/42, nested layouts, access checks, loading data before a view appears, and lazy loading code. The Angular Router handles all of these.
What the Angular Router Does
When a navigation starts, from a link, from code, or from the Back button, the router runs a pipeline:
- It parses the URL into a
UrlTree, a structured form holding the path segments, query parameters, and fragment. - It matches the segments against your route configuration and builds a tree of route states.
- It runs guards, which can allow, block, or redirect the navigation.
- It runs resolvers, which fetch data before the view appears.
- It activates the matched components inside router outlets.
- It updates the browser URL and emits events as each stage completes.
Each later chapter covers one of these stages in detail.
Exercise
A user fills in half a form on /checkout, clicks an in-app link to /cart, then presses Back. Why does an SPA keep the application's in-memory state here when a multi-page site would not, and which browser event lets the router react to the Back press?
Show answer
The SPA never unloads its document. The router swaps components and calls history.pushState, so services and other JavaScript state stay alive. A multi-page site loads a new document and loses that state. The Back press fires the popstate event, which the router listens for so it can run a new navigation. Form fields on the destroyed component are still lost unless their values are kept in a service.