liveBook · Manning

Routing

As explained in Vanilla Web

Comparing a navigation destination with route definitions to select a view or handler.

Routing decides which content answers a URL. In a multi-page application, the server can make that decision and return a document. In a single-page application, JavaScript can intercept an internal navigation and update the current document. Either way, the browser still owns URLs, history, Back and Forward, bookmarks, and reloads. A client-side router should work with those behaviors, not simulate them separately. section 10.1 section 10.1.3

Keep navigation in real <a href> links. Intercept only eligible same-origin routes; leave external links, downloads, and other navigations to the browser. A link should still work without interception, and the server must answer a valid deep link opened directly or reloaded. Public content may also need server-rendered or prerendered HTML, canonical URLs, and genuine HTTP 404 responses. section 10.1.1 section 10.3.2

Keep the router’s responsibilities separate:

  • Define route patterns, then compare the destination path with them and select the first match. A literal path such as /cart selects one view; /products/:id also extracts a parameter. URLPattern handles matching in current browsers, with a small matcher or regular-expression fallback for older ones. section 10.4 section 15.4
  • After a match, call its handler or load its page. Cancel pending work from the previous route, then render the new view or an appropriate pending, empty, error, or not-found state. section 10.1.3 section 15.4
  • Finish the navigation by updating the title, active links, focus, and scroll. A same-document update does not automatically provide all the cues of a new document. section 11.1.3 section 11.1.4

The Navigation API provides a central navigate event for modern same-document routing. Check whether the event can be intercepted and whether its destination matches before calling intercept(). Its signal can cancel route work; manual focus and scroll settings let the router act after the new view exists. Where the API is unavailable, a History API fallback must handle eligible link clicks and Back and Forward traversal explicitly. URLPattern and the Navigation API are useful defaults, not universal availability assumptions. section 10.3.2 section 10.5

Listing 10.1 Using the Navigation API to intercept same-document navigations
if ("navigation" in globalThis) {
  navigation.addEventListener("navigate", (event) => {
    if (!event.canIntercept || event.hashChange) return; #1
    if (event.downloadRequest) return; #1

    const url = new URL(event.destination.url);
    if (url.origin !== location.origin) return; #1

    // matchRoute is defined in a separate function.
    const match = matchRoute(url.pathname);
    if (!match) return; #2

    event.intercept({
      focusReset: "manual", #3
      scroll: "manual", #3

      async handler() {
        const page = await match.handler(match.params, {
          signal: event.signal #4
        });
        renderPage(page); #5

        document.title = page.title;
        focusMainHeading(); #6

        event.scroll(); #7
      }
    });
  });
}

The interception example checks eligibility and origin, matches the path, passes the navigation signal to the handler, and restores title, focus, and scroll after rendering. section 10.3.2

URL-backed tabs

Give each account tab a real link and a route: /account/profile, /account/billing, and /account/security. Route matching replaces the outlet with the selected view, while the URL lets users bookmark a tab, reload it, or revisit it with Back and Forward. Three links and three handlers need no nested routing system. section 11.4.3

How Vanilla Web covers it

How deep it goes
Works through route patterns, interception, fallbacks, rendering responsibilities, and accessibility rather than stopping at path matching.
Examples
Uses product paths with named parameters, URL-backed account tabs, and a route table that lazy-loads page components.
What it assumes
Starts with URLs and links, then introduces route patterns and native interception without requiring a framework router.
What you can do after
Build it from scratch using real links, matched routes, browser navigation APIs, fallbacks, and accessible view updates.
How it connects
Connects URLPattern, the Navigation API, History API, deep links, Web Components, and focus management.

Also explained in

Compare how 34 books explain routing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Vanilla Web. The section links point to the parts of the book it draws on.