liveBook · Manning

Chapter 11: Building a router

What you will learn in chapter 11 of Vanilla Web

The chapter previews building a client-side router from matched routes, including rendering, browser-cue updates, and view transitions.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter previews building a client-side router from matched routes, including rendering, browser-cue updates, and view transitions.

11.1 Rendering routes

This section explains what must be decided before a matched route is rendered and sketches a simple contract for rendering it.

Key terms: route, route pattern, parameters, query values, render contract, route handler, title, DOM node, document metadata, active links

11.1.1 Outlet and master pages

Explains how a router renders changing route content inside a replaceable outlet while keeping the surrounding app shell in place.

Key terms: outlet, app shell, master page, #route-outlet

11.1.2 Using DOM APIs

This section compares replacing an outlet’s DOM content with showing and hiding prebuilt views, and recommends replacement for application routes.

Key terms: replaceChildren(), outlet, DOM replacement, show/hide, route-level rendering, hidden route trees

11.1.3 Title and metadata

Explains which document details a router must update after rendering a route and why public-facing metadata also needs a server-provided source.

Key terms: same-document navigation, document.title, canonical URL, active navigation state, social sharing metadata, server-rendered, statically generated metadata, discoverability

11.1.4 Active navigation and focus

Explains how a client-side router restores navigation cues by marking the active link and moving focus after a route change.

Key terms: aria-current="page", aria-current="true", exact-match comparison, path prefix, same-document navigation, focus, heading, <main>, tabindex="-1", tabindex

11.1.5 Scroll restoration

Explains the scroll behavior users expect from a router and how to restore the right position at the right time.

Key terms: new route, Back or Forward, hash fragment, scroll position, history.scrollRestoration, auto, manual, Navigation API, event.scroll(), history entry state

11.1.6 Using web components

This subsection shows how a router can select Web Component pages, pass them route data and cancellation signals, and leave each component responsible for its own UI.

Key terms: Web Component, router, app shell, route data, outlet replacement, event.signal, AbortSignal, route handler, page component

11.1.7 Route guards

This section explains how route guards check a destination before rendering and either allow, cancel, or redirect navigation.

Key terms: route guard, authentication, navigate listener, event.preventDefault(), pushState, popstate, meta property, requiresAuth

11.1.8 Not found and route errors

Distinguishes an unknown route from a failure in a matched route and explains why each needs different handling.

Key terms: Unknown route, Route error, not-found view, server 404

11.2 Navigating to new routes

Explains how to start route changes from JavaScript, choose history behavior, and store small amounts of route-local state without replacing shareable URL state.

Key terms: real links, navigation.navigate(), navigate event, event.intercept(), committed, finished, History API, pushState(), replaceState(), popstate

11.3 View transitions for route change

Introduces View Transitions as an optional way to smooth route changes and distinguishes transitions within a document from those between documents.

Key terms: View Transitions, routing, same-document view transitions, cross-document view transitions, SPA, MPAs, hybrid apps, same-origin

11.3.1 View Transitions API

This section explains how to animate a same-document route update with the View Transitions API while keeping animation optional.

Key terms: View Transitions API, document.startViewTransition(), callback, same-document transitions, root transition, ::view-transition-old(root), ::view-transition-new(root), view-transition-name, progressive enhancement, opacity

11.3.2 Cross-document view transitions

This section explains how browsers can animate eligible same-origin navigations between separate documents while preserving normal document navigation.

Key terms: cross-document view transitions, same-origin navigation, document.startViewTransition(), opt in, same origin, cross-origin redirects, Baseline status, progressive enhancement, normal document navigation

11.3.3 Motion and accessibility

This section explains how to keep route transitions accessible, especially for users who prefer reduced motion.

Key terms: reduced motion, route transitions, prefers-reduced-motion, View Transitions, ::view-transition-group(*), document.startViewTransition()

11.4 Building a tiny Vanilla router

Introduces a two-pass plan for building a small Vanilla router, starting with a function and later refactoring it into a class.

11.4.1 First pass: a function-based router

Introduces a minimal function-based router for back-and-forward navigation and explains when its limitations justify a class.

Key terms: matcher, outlet, render function, popstate, link clicks, Navigation API, focus, scroll, transitions, class

11.4.2 Second pass: a class-based router

This section outlines a small class-based router’s contract and rendering flow, then explains two interception details and why the router remains deliberately limited.

Key terms: handler, title, view, Node, #getCurrentPath(url?), #resolve(path), addRoute(path, handler), Navigation, History API, URLPattern

11.4.3 Using the router for a tab view

This section uses a tab view to show how a small router can map real links and URLs to views.

Key terms: route, URL, Back and Forward, real links, route matching, CSS, route handlers

11.5 Building a web component-based router

This section explains how an existing routing approach fits into a web component architecture by assigning responsibilities to an app shell, router, and page components.

Key terms: app shell, outlet, router module, page components, real links, route matching, metadata updates, focus management, cancellation

11.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 10: Navigation Chapter 12: Working with data →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.