Chapter 10: Navigation
Introduces navigation as movement between views and routing as matching a URL to content, while explaining why SPA routing still depends on the browser and server.
After this chapter you will be able to
- Navigation and router roles. Distinguish navigation from routing: navigation moves between screens, while a router matches the route expressed in the URL to the content the application should show.
- Browser and server responsibilities. Explain why client-side routing is broader than changing a URL and rendering a view: even with an SPA focus, the browser’s history, server support for deep links, and accessible, crawlable links still matter.
- Routing across web architectures. Explain what routing decides and distinguish how multi-page, single-page, and hybrid applications handle navigation.
- Browser navigation integration. Explain why a router must respect the browser’s URL and navigation behavior rather than merely changing visible content.
- URL changes for view history. Apply the section’s rule for deciding whether a JavaScript-driven main-view change should update the URL.
- Discoverable public routes. Describe the URL, link, and HTTP choices recommended for public or indexable content.
- Meaningful HTML without client rendering. Explain why important discoverable content should have a simple path from its URL to meaningful HTML, despite JavaScript-capable crawlers.
- Graceful navigation degradation. Describe what should continue to work when client-side navigation enhancements or in-memory state are unavailable.
- Native navigation triggers and default. Describe ways navigation can begin before router code runs, such as following a link, submitting a form, using browser controls, or calling a JavaScript navigation API. Explain that the browser normally tries to load a new document through HTTP unless JavaScript intercepts the navigation.
- Native document replacement effects. Explain what happens to the current window's document during a native navigation and name the effects on JavaScript context, application state, focus, and possibly scroll position.
- Selective SPA navigation interception. Explain that SPA routing intercepts some same-origin navigations and handles them as same-document updates rather than replacing native navigation.
- Client-side router responsibilities. Describe the router’s responsibilities at the level listed here: match the URL, cancel previous-route work, optionally show a pending state, load data, render the resulting state, and update page and navigation context.
- URLs and route-specific views. Explain that navigation and routing concern URLs and the different views or content rendered for a specific route.
- Friendly URL purpose and design. Explain what makes a URL friendly, why it matters, and when a descriptive path is preferable to query parameters.
- Real URL criteria. Explain why a URL shown by a client-side router must also work when opened directly, refreshed, or shared, without relying on prior clicks.
- Real link requirements. Distinguish a real link from an element that navigates only through JavaScript, and explain what native behavior a valid href provides.
- Progressive router enhancement. Explain how a client-side router can enhance real links while leaving ordinary browser navigation available.
- Path query and hash roles. Distinguish the path as the resource’s main route, the query string as optional key/value state for a variation of the view, and the hash as a fragment target within the current document. Explain why query state is useful when it should survive reloads or be shared or bookmarked.
- URL and route distinction. Distinguish a URL from a route: the URL is an address, while the route maps a URL pattern to the code or document that renders matching content.
- Named path parameter matching. Explain how `/products/:slug` matches `/products/coffee-beans` and captures `coffee-beans` as the named parameter `slug`.
- Meaningful durable route URLs. Explain why good routes use descriptive paths, put shareable optional UI state in the query string, reserve hashes for document fragments, and retain their meaning when shared or reopened.
- Canonical route shape. Explain why routes that represent the same view should have one preferred shape, and describe the URL conventions that must be chosen deliberately.
- Public canonical URL purpose. Explain why canonical route choices matter beyond route matching and how public pages identify a preferred URL for the same content.
- Shareable route state. Explain why filters, sorting, pagination, view mode, and search text belong in the query string when they change a view without identifying a different resource.
- Browser history entry choice. Distinguish query-state normalization from a change the user would expect Back to undo when deciding whether to add a history entry.
- Server First Navigation Sequence. Trace what happens when someone opens an SPA URL directly: the browser requests that path from the server, which must respond before the client-side router can read the URL and render a view.
- Selective SPA Server Responses. Explain how the server should distinguish valid client-rendered app routes from files, API endpoints, and missing routes. Explain why returning the shell with a 200 OK for every unknown path creates a soft 404.
- Full-page versus intercepted navigation. Distinguish a browser following a link to a new document from a client-side router intercepting navigation to update the URL and render another view in the current document.
- Navigation and History API roles. Explain why the Navigation API provides a cleaner central model for interception and what extra work a History API fallback requires.
- Navigation entry list scope. Explain that the navigation entry list is the current tab’s session history as exposed to the page, not the user’s complete browser history.
- Documents and Back traversal. Distinguish entries for different server-loaded documents from client-side route entries in the same document, and describe what Back does in the section’s SPA example.
- Navigation and History API visibility. Distinguish the Navigation API’s view of entries from the History API’s more limited operations, and explain the consequence for routers.
- Navigation API interception flow. Explain how the global navigation object’s navigate event lets a router handle different navigation sources, and what changes when an eligible navigation is intercepted.
- Manual focus and scroll control. Describe the router’s responsibilities when interception uses manual focus reset and scrolling, including when to call event.scroll().
- Cancellation of pending route work. Explain how event.signal cancels pending work when a new navigation supersedes the current one, including an in-progress fetch.
- Silent handling of navigation aborts. Explain why an aborted fetch should be handled silently in the navigation handler rather than displayed as a router error.
- Form submission navigation data. Explain that the Navigation API fires a navigate event for form submissions, with GET fields in the destination URL’s query parameters and POST data in event.formData.
- Navigation completion signals. Explain how to tell whether an intercepted navigation succeeded or failed without expecting a promise from event.intercept().
- History API navigation behavior. Explain how pushState(), replaceState(), and popstate relate to session history, and why a fallback must handle programmatic updates and link clicks separately.
- Safe fallback link handling. Justify starting a fallback with real links and intercepting only clear same-document application navigations, while preserving ordinary browser link behavior.
- Route selection by path. Explain that after client-side navigation is intercepted, the router compares the current URL path with a list of route patterns and selects the first match.
- Static and parameterized paths. Distinguish a pattern without parameters, such as /cart, from one with a named path parameter, such as /products/:id; explain how a match supplies the parameter’s value.
- Basic matching and its limits. Explain that a very small app can match a few static paths with a hardcoded switch statement and handle a dynamic product path manually, but that this approach becomes difficult to maintain and lacks segment-boundary validation and URL-parameter decoding.
- Pathname route matching. Explain how a route pattern is compiled into a matcher that receives the current path and returns extracted parameters or null.
- Route order and fallback. Explain why specific routes should precede generic ones when selecting the first match, and describe the final not-found route.
- Isolated route matcher testing. Explain why a Vanilla route matcher can be unit-tested without a DOM, browser, or framework runtime.
What it builds on
- Screen readers (chapter 3) · Screen-reader interaction with accessible content.
- Application architecture (chapter 2) · The distinction between web application architecture choices.
Section by section
Introduction
Introduces navigation as movement between views and routing as matching a URL to content, while explaining why SPA routing still depends on the browser and server.
- Navigation and router roles: Distinguish navigation from routing: navigation moves between screens, while a router matches the route expressed in the URL to the content the application should show.
Why it matters: This distinction gives you a way to describe what a router contributes to moving around an app. - Browser and server responsibilities: Explain why client-side routing is broader than changing a URL and rendering a view: even with an SPA focus, the browser’s history, server support for deep links, and accessible, crawlable links still matter.
Why it matters: It keeps the router from being mistaken for a replacement for the rest of web navigation. - View and handler terminology: Distinguish a view, a page component, and a route handler as this chapter uses those terms, including what the handler produces when a route matches.
Why it matters: These terms make it easier to discuss matching a route separately from rendering its content.
Key terms: navigation, router, route, URL, view, client-side router, browser’s native history, deep links, accessible and crawlable links, page component
10.1 Navigation and routing on the Web
Defines routing, compares where navigation decisions happen, and explains why view changes must work with browser URLs and history.
- Routing across web architectures: Explain what routing decides and distinguish how multi-page, single-page, and hybrid applications handle navigation.
Why it matters: This distinction clarifies whether a navigation loads a document or updates the current one. - Browser navigation integration: Explain why a router must respect the browser’s URL and navigation behavior rather than merely changing visible content.
Why it matters: A view that looks navigable may still fail to behave like a web destination. - URL changes for view history: Apply the section’s rule for deciding whether a JavaScript-driven main-view change should update the URL.
Why it matters: The URL must reflect view changes that users expect to undo with Back.
Key terms: routing, multi-page application (MPA), single-page application (SPA), hybrid architecture, HTML shell, router, address bar, back and forward buttons, bookmarks, history traversal
10.1.1 Discoverability, accessibility, and graceful degradation
The section explains how routing choices affect discoverability and accessibility, and why navigation should keep working when client-side enhancements are unavailable.
- Discoverable public routes: Describe the URL, link, and HTTP choices recommended for public or indexable content.
Why it matters: A route's URL, link, and HTTP response matter beyond the app's JavaScript. - Meaningful HTML without client rendering: Explain why important discoverable content should have a simple path from its URL to meaningful HTML, despite JavaScript-capable crawlers.
Why it matters: A direct path to meaningful HTML also helps tools that fetch or summarize pages programmatically. - Graceful navigation degradation: Describe what should continue to work when client-side navigation enhancements or in-memory state are unavailable.
Why it matters: A clean router must preserve ordinary browser navigation rather than make it depend on enhancements.
Key terms: readable paths, fragment-based routing, <a href> links, canonical URLs, HTTP status codes, SPA-only rendering, server-rendered, statically rendered, prerendered HTML, graceful degradation
10.1.2 Native navigation
This section explains what triggers browser navigation by default and what a native navigation replaces.
- Native navigation triggers and default: Describe ways navigation can begin before router code runs, such as following a link, submitting a form, using browser controls, or calling a JavaScript navigation API. Explain that the browser normally tries to load a new document through HTTP unless JavaScript intercepts the navigation.
Why it matters: A client-side router builds on navigation the browser already provides rather than creating navigation from nothing. - Native document replacement effects: Explain what happens to the current window's document during a native navigation and name the effects on JavaScript context, application state, focus, and possibly scroll position.
Why it matters: Replacing the document affects more than the visible page content.
Key terms: native navigation, client-side router, JavaScript intercepts, JavaScript context, application state, focus, scroll position
10.1.3 Client-side router
The section frames SPA routing as selective interception of native navigation and outlines the responsibilities of a client-side router.
- Selective SPA navigation interception: Explain that SPA routing intercepts some same-origin navigations and handles them as same-document updates rather than replacing native navigation.
Why it matters: This distinction helps you reason about what the router handles and what the browser still handles. - Links and deep links: Describe what must remain functional when JavaScript intercepts internal navigation: external links use normal browser navigation, internal links remain real links, and deep links work on reload or in a new tab.
Why it matters: Routing should not make links or directly opened URLs depend on an earlier in-app click. - Client-side router responsibilities: Describe the router’s responsibilities at the level listed here: match the URL, cancel previous-route work, optionally show a pending state, load data, render the resulting state, and update page and navigation context.
Why it matters: This checklist shows that routing involves more than choosing which content to display.
Key terms: SPA routing, same-origin navigations, same-document updates, native navigation, external links, internal links, real links, deep links, client-side router, route data
10.1.4 Navigation and routing definitions
The section frames navigation and routing as using URLs to show content for a particular route.
- URLs and route-specific views: Explain that navigation and routing concern URLs and the different views or content rendered for a specific route.
Why it matters: This gives you the basic purpose of routing before the book develops its terminology and implementation.
Key terms: navigation, routing, URLs, route, views
10.1.5 Friendly URLs
This section explains how descriptive URLs communicate a resource’s meaning and why they are preferable to opaque URLs.
- Friendly URL purpose and design: Explain what makes a URL friendly, why it matters, and when a descriptive path is preferable to query parameters.
Why it matters: A descriptive URL helps people and tools recognize, share, and find a resource without first running the app.
Key terms: friendly URL, clean URL, human-readable URL, descriptive path, query parameters
10.1.6 Real URLs
This section defines a real URL by whether it reaches the correct content directly and explains how SPAs and MPAs support that behavior.
- Real URL criteria: Explain why a URL shown by a client-side router must also work when opened directly, refreshed, or shared, without relying on prior clicks.
Why it matters: A reader can use this test to tell whether a route is genuinely shareable. - SPA and MPA URL handling: Distinguish how an SPA and an MPA usually make direct requests to their URLs work.
Why it matters: The distinction explains how either architecture can make a pasted or refreshed URL reach the right content.
Key terms: real URL, native navigation, temporary client-side state, SPA, MPA, application shell, client-side router
10.1.7 Real links
This section explains why navigation should start with real HTML links and how a client-side router can enhance them without losing native behavior.
- Real link requirements: Distinguish a real link from an element that navigates only through JavaScript, and explain what native behavior a valid href provides.
Why it matters: A visible view change alone does not give users and other agents the capabilities of a link. - Progressive router enhancement: Explain how a client-side router can enhance real links while leaving ordinary browser navigation available.
Why it matters: The router can provide in-app navigation without becoming the only way a link works. - Links around card content: Describe the accessibility and markup trade-offs of wrapping card content in a link and the alternative suggested for complex cards.
Why it matters: Card designs need a usable link without turning all their content into an unwieldy link label. - Resolved link destinations: Explain why router code should read link.href rather than getAttribute("href") when resolving a link destination.
Why it matters: Using the resolved URL keeps router navigation aligned with the browser’s actual link destination.
Key terms: real link, <a>, href, progressively enhance, client-side router, normal clicks, browser history, card link, ::before, aria-labelledby
10.1.8 URL parts
Distinguishes the path, query string, and hash in a URL and explains why hash routing is generally discouraged as a primary strategy.
- Path query and hash roles: Distinguish the path as the resource’s main route, the query string as optional key/value state for a variation of the view, and the hash as a fragment target within the current document. Explain why query state is useful when it should survive reloads or be shared or bookmarked.
Why it matters: These distinctions help you choose which part of a URL should represent a route, view state, or location within a document. - Primary hash routing drawbacks: Justify the advice against using hash routing as the primary strategy for public pages and modern apps, using the section’s stated drawbacks.
Why it matters: It explains why a fragment can be useful without making hash-based URLs the default shape of public routes.
Key terms: path, query string, hash, fragment, hash routing, native URL model, server integration, discoverability
10.1.9 Route
This section defines a route, illustrates a named path parameter, and explains where routes are resolved and what makes their URLs useful.
- URL and route distinction: Distinguish a URL from a route: the URL is an address, while the route maps a URL pattern to the code or document that renders matching content.
Why it matters: This distinction makes it easier to explain what a router matches and what the application renders. - Named path parameter matching: Explain how `/products/:slug` matches `/products/coffee-beans` and captures `coffee-beans` as the named parameter `slug`.
Why it matters: A single route pattern can match paths whose value in a named position differs. - Route resolution by architecture: Describe where routes are usually resolved in an MPA, an SPA, and a hybrid app, and what the MPA and SPA do when resolving them.
Why it matters: The same idea of a route applies whether the server, client-side JavaScript, or both handle it. - Meaningful durable route URLs: Explain why good routes use descriptive paths, put shareable optional UI state in the query string, reserve hashes for document fragments, and retain their meaning when shared or reopened.
Why it matters: Meaningful route URLs let people share and revisit the same destination without losing its meaning.
Key terms: URL, route, URL pattern, route pattern, named path parameter, slug, MPA, SPA, hybrid app, server
10.1.10 Canonical route shapes
Explains how to choose consistent route URLs and identify preferred public URLs without creating duplicate variants.
- Canonical route shape: Explain why routes that represent the same view should have one preferred shape, and describe the URL conventions that must be chosen deliberately.
Why it matters: A consistent URL shape keeps the router from treating multiple spellings of the same view as different routes. - Public canonical URL purpose: Explain why canonical route choices matter beyond route matching and how public pages identify a preferred URL for the same content.
Why it matters: A preferred public URL helps avoid duplicate-content variants while keeping routing and troubleshooting simpler. - Language-specific canonical URLs: Distinguish canonical URLs for translated public pages from links between equivalent language versions.
Why it matters: Translated pages are related versions, not URL duplicates that should all declare one language's page canonical. - Initial canonical HTML: Explain the respective roles of the initial HTML response and client-side updates to a canonical link.
Why it matters: Client-side updates keep the URL metadata in step with navigation, but public pages should not rely on those updates alone.
Key terms: canonical shape, normalize, trailing slashes, query parameters, canonical URL, public content, discoverability, hreflang, multilingual public content, initial HTML response
10.1.11 Route state
Explains how to represent shareable route state in query strings and decide whether changes should create browser history entries.
- Shareable route state: Explain why filters, sorting, pagination, view mode, and search text belong in the query string when they change a view without identifying a different resource.
Why it matters: A shared URL can lead someone to the same filtered view. - Query defaults and canonicalization: Describe how defaults and consistent query-parameter ordering keep URLs cleaner and more consistent.
Why it matters: Consistent URLs avoid unnecessary variations of the same view. - Browser history entry choice: Distinguish query-state normalization from a change the user would expect Back to undo when deciding whether to add a history entry.
Why it matters: This keeps the Back button useful rather than filling it with minor URL adjustments.
Key terms: route state, query string, defaults, url.searchParams.sort(), canonicalizing query strings, history, Back, normalizing state
10.2 The server
This section explains why the server must handle direct URL requests correctly before a client-side router can render a view.
- Server First Navigation Sequence: Trace what happens when someone opens an SPA URL directly: the browser requests that path from the server, which must respond before the client-side router can read the URL and render a view.
Why it matters: It explains why a route that works inside an app may fail when opened directly. - Selective SPA Server Responses: Explain how the server should distinguish valid client-rendered app routes from files, API endpoints, and missing routes. Explain why returning the shell with a 200 OK for every unknown path creates a soft 404.
Why it matters: A selective rewrite lets real URLs work without breaking other requests or disguising missing pages. - Subpath Deployment Alignment: Describe how deployment under a path such as /dashboard/ affects both the server rewrite and the client router, and state the optional role of an HTML base element for asset loading.
Why it matters: Matching the deployment path on both sides prevents routes from failing only after deployment. - Route Specific HTML Alternatives: Distinguish a shell-only SPA response from SSR and SSG responses, and explain why route-specific HTML can better suit public content whose discoverability matters.
Why it matters: The SPA shell rewrite is useful, but it is not automatically the best response for public pages. - Intentional Authenticated Route Responses: Describe the two server-response strategies given for an authenticated route and explain why a blanket rewrite needs care with private or stale information.
Why it matters: Authenticated URLs still need a deliberate server response, even when the app uses client-side routing.
Key terms: server, client-side router, shell document, SPA entry document, rewrite rule, API route, 404 Not Found, soft 404, subpath, base path
10.3 Navigation interception
This section contrasts full-page navigation with client-side interception and introduces the two browser APIs used to manage navigation.
- Full-page versus intercepted navigation: Distinguish a browser following a link to a new document from a client-side router intercepting navigation to update the URL and render another view in the current document.
Why it matters: This distinction explains what navigation interception is meant to accomplish. - Navigation and History API roles: Explain why the Navigation API provides a cleaner central model for interception and what extra work a History API fallback requires.
Why it matters: The difference clarifies why the two interception approaches need different amounts of application code.
Key terms: multi-page application, client-side routed application, intercept, Navigation API, History API, history.pushState(), popstate
10.3.1 Navigation entry list
This section explains what the Navigation API’s entry list represents, how Back and Forward traverse its entries, and how its visibility differs from the History API.
- Navigation entry list scope: Explain that the navigation entry list is the current tab’s session history as exposed to the page, not the user’s complete browser history.
Why it matters: A router must not treat the entry list as unrestricted access to browser history. - Documents and Back traversal: Distinguish entries for different server-loaded documents from client-side route entries in the same document, and describe what Back does in the section’s SPA example.
Why it matters: Back navigation can require a view change even when the browser keeps the same document. - Navigation entry metadata: Describe the entry metadata named here, especially the difference between an entry’s key and index.
Why it matters: An entry’s identity and its position answer different questions about navigation state. - Navigation and History API visibility: Distinguish the Navigation API’s view of entries from the History API’s more limited operations, and explain the consequence for routers.
Why it matters: This distinction explains why a router using the History API cannot inspect the session’s entries in the same way.
Key terms: session history, navigation entry list, browsing context, same-origin entries, multi-page application, client-side routed application, same document, traversal, URL, index
10.3.2 The Navigation API
Introduces the Navigation API as a central way to intercept navigations for same-document routing and explains the router’s resulting rendering, focus, and scroll responsibilities.
- Navigation API interception flow: Explain how the global navigation object’s navigate event lets a router handle different navigation sources, and what changes when an eligible navigation is intercepted.
Why it matters: This division of work is the basis for routing without replacing the current document. - Navigation API browser support: State why the Navigation API is a useful modern default without assuming it is available everywhere.
Why it matters: A router can favor the simpler modern API while accounting for environments that lack it. - Manual focus and scroll control: Describe the router’s responsibilities when interception uses manual focus reset and scrolling, including when to call event.scroll().
Why it matters: Rendering the view first lets focus and scrolling land in the right place for the navigation.
Key terms: Navigation API, navigation, navigate, event.intercept(), same-document navigation, Baseline 2026, focusReset: "manual", scroll: "manual", event.scroll()
10.3.3 Cancellation with event.signal
This section explains how the Navigation API cancels work from a superseded navigation and how to treat the resulting abort without showing an error.
- Cancellation of pending route work: Explain how event.signal cancels pending work when a new navigation supersedes the current one, including an in-progress fetch.
Why it matters: A route change should not leave work from the previous route running. - Silent handling of navigation aborts: Explain why an aborted fetch should be handled silently in the navigation handler rather than displayed as a router error.
Why it matters: A user choosing a different route should not see an error for the work they left behind.
Key terms: Navigation API, event.signal, AbortSignal, AbortError, navigateerror
10.3.4 Form submissions
Explains how the Navigation API exposes GET and POST form submissions to a router, and why a History API fallback needs separate form handling.
- Form submission navigation data: Explain that the Navigation API fires a navigate event for form submissions, with GET fields in the destination URL’s query parameters and POST data in event.formData.
Why it matters: This lets a router handle search forms, login forms, and other form-driven flows. - History API form limitation: Distinguish the Navigation API’s form handling from the History API fallback: the History API cannot directly intercept native form navigation, so older-browser support can use separate submit listeners on forms.
Why it matters: A router that relies on the fallback must add form handling explicitly if it needs it.
Key terms: Navigation API, navigate, event.destination.url, event.formData, History API fallback, native browser navigation, submit event
10.3.5 Observing completion
Explains how to observe intercepted navigation outcomes, why Navigation API events simplify interception, and why production routers still need a fallback.
- Navigation completion signals: Explain how to tell whether an intercepted navigation succeeded or failed without expecting a promise from event.intercept().
Why it matters: A router needs a separate way to observe the outcome of an intercepted navigation. - Direct navigation events: Explain why the Navigation API is cleaner than manually intercepting clicks.
Why it matters: It explains what work the browser handles when a router uses the Navigation API. - Production router fallback: Explain why a production Vanilla router cannot rely exclusively on the Navigation API and name the fallback given here.
Why it matters: It keeps the router usable where the Navigation API is unavailable.
Key terms: event.intercept(), navigatesuccess, navigateerror, navigation.transition, finished, Navigation API, navigation event, manual click interception, feature detection, History API
10.3.6 History API fallback
This section explains what a History API navigation fallback can do, what it must handle itself, and why it cannot fully replace the Navigation API.
- History API navigation behavior: Explain how pushState(), replaceState(), and popstate relate to session history, and why a fallback must handle programmatic updates and link clicks separately.
Why it matters: These event gaps determine what a History API router has to handle itself. - Safe fallback link handling: Justify starting a fallback with real links and intercepting only clear same-document application navigations, while preserving ordinary browser link behavior.
Why it matters: Selective interception keeps a fallback from breaking navigation people expect links to provide. - Polyfill and ponyfill limits: Distinguish a Navigation API polyfill from a ponyfill and describe the limits and checks involved in using either as a fallback.
Why it matters: A library can spare you a custom fallback, but its API and emulated behavior still need scrutiny.
Key terms: History API, history.pushState(), history.replaceState(), popstate, real links, same-document application navigations, modifier keys, hash jumps, idempotent GET forms, polyfill
10.4 Route matching
Introduces how a client-side router selects a route from the current path, including static and parameterized patterns and the browser APIs available for matching them.
- Route selection by path: Explain that after client-side navigation is intercepted, the router compares the current URL path with a list of route patterns and selects the first match.
Why it matters: This is the decision a router must make before handling an intercepted navigation. - Static and parameterized paths: Distinguish a pattern without parameters, such as /cart, from one with a named path parameter, such as /products/:id; explain how a match supplies the parameter’s value.
Why it matters: Named parameters let one route pattern represent paths for different individual items. - Native matching and fallback: Describe URLPattern as a native way to express route patterns and extract URL parameters, and state when a small regular-expression fallback may be useful.
Why it matters: Browser support affects which matching approach is appropriate for an app.
Key terms: route matching, route patterns, current URL path, named path parameter, dynamic, route pattern, URLPattern, Baseline 2025, regular expression fallback
10.4.1 Simple matching
This subsection presents hardcoded route matching as a learning-friendly option for very small apps and explains why it does not scale well.
- Basic matching and its limits: Explain that a very small app can match a few static paths with a hardcoded switch statement and handle a dynamic product path manually, but that this approach becomes difficult to maintain and lacks segment-boundary validation and URL-parameter decoding.
Why it matters: It helps you recognize when a learning example is too limited for more complex routes.
Key terms: hardcoded switch statement, static paths, dynamic product path, segment boundaries, URL parameters
10.4.2 Matching routes with URLPattern
This section explains how to match URL paths to routes with URLPattern or a limited regex fallback, then choose a route in a safe order.
- Pathname route matching: Explain how a route pattern is compiled into a matcher that receives the current path and returns extracted parameters or null.
Why it matters: This gives the router a consistent way to tell whether a path matches and to obtain its parameters. - Regex fallback limits: Distinguish the regex fallback's supported route patterns from URLPattern's broader syntax, and state how to keep patterns safe for the fallback.
Why it matters: A route pattern that works with URLPattern may not work with this simpler fallback. - Route order and fallback: Explain why specific routes should precede generic ones when selecting the first match, and describe the final not-found route.
Why it matters: Ordering prevents a general route from stealing a specific path and ensures unmatched paths get a not-found result. - Path and query distinction: Distinguish the pathname commonly used to identify a client-side route from query parameters usually parsed as optional state.
Why it matters: It clarifies which part of a URL normally determines the route and which part can supply optional state.
Key terms: URLPattern, pathname, exec(), named groups, matcher function, regex fallback, literal segments, :name parameters, route order, first route
10.4.3 Testing the matcher
The section explains why a small Vanilla route matcher is easy to test in isolation and how testing changes for browser-dependent routing behavior.
- Isolated route matcher testing: Explain why a Vanilla route matcher can be unit-tested without a DOM, browser, or framework runtime.
Why it matters: Keeping the matcher independent makes route matching straightforward to verify. - Router testing boundaries: Distinguish router parts that can be unit-tested separately from browser-dependent behavior that may need mocks or a real-browser test.
Why it matters: This distinction helps you choose a practical test approach for each part of a router.
Key terms: matcher, test in isolation, unit-tested, View Transitions, browser navigation event
10.5 Summary
Where this is used later
- Rendering routes (chapter 11) · Connects a matched route to the view rendered for it.
- Navigating to new routes (chapter 11) · Provides the criterion for updating the URL when navigating to a new route.
- View transitions for route change (chapter 11) · Route transitions are considered as an enhancement rather than a requirement for navigation.
- Scroll restoration (chapter 11) · Provides context for why scroll position matters when handling route changes.
- Outlet and master pages (chapter 11) · The idea of a client-side router changing the displayed route.
- The document metadata (chapter 15) · Nested routes as real URLs.
- Building a tiny Vanilla router (chapter 11) · Building a router calls for route URLs that remain meaningful outside an in-app navigation.
- Title and metadata (chapter 11) · Route rendering can set title and metadata appropriate to the current page.
Concepts explained in this chapter
- routingComparing a navigation destination with route definitions to select a view or handler.
- Navigation APIA browser API for observing and controlling navigation, including intercepting navigations.
- URLPatternURLPattern is a web-platform API for matching URLs against structured patterns and extracting their components or parameters.
- History APIThe History API is a browser API for manipulating session history entries and navigating between document URLs without necessarily loading new documents.
- query stringA query string is the URL component containing name-value parameters that modify a resource request or represent view state.
- server-side renderingGenerating HTML on a server for delivery to a browser.
- url fragmentA hash fragment is the fragment identifier following a URL's hash mark that identifies a target within a document.
- popstate eventA browser event dispatched when the active session-history entry changes through history traversal.
- canonical URLA canonical URL is the preferred URL designated as the authoritative address for a page or resource among equivalent URL variants.
- route handlerA route handler is a function or component associated with a route that produces the content or response for that destination.
- prerenderingStatic site generation is the process of producing deployable HTML files from templates and content before those files are served to users.
- application shellAn application shell is the persistent structure and shared interface of an application around content that changes or loads separately.
- rewrite ruleA rewrite rule is server configuration that maps an incoming request path to a different resource or response without changing the requested URL.
- serverA networked system that responds to client requests by returning resources or providing services.
- single-page applicationA single-page application is a web application that updates its interface within one loaded document rather than performing full document loads for each view.
- navigate eventA Navigation API event dispatched when a navigation is initiated.
- event.intercept()event.intercept() is a navigation-event method that prevents a supported navigation from proceeding natively and assigns application handling to it.
- history.pushState()history.pushState() adds a session-history entry with optional state and URL without loading a new document.
- history.replaceState()A History API method that replaces the current session-history entry and can change the displayed URL without loading a new document.
- fallbackAn alternative implementation or behavior used when a preferred feature or supplied content is unavailable.
- URLA URL is a standardized textual address that identifies a web resource or navigation destination and can include protocol, host, port, path, query, and fragment components.
- navigation modelA navigation model is an architectural scheme that determines how URLs and user navigation map to web application content or views.
- navigationMovement among locations or views in a web application.
- AbortSignalA web-platform interface that communicates cancellation of an asynchronous operation.
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.
