liveBook · Manning

Route handler

As explained in Vanilla Web

A route handler is a function or component associated with a route that produces the content or response for that destination.

A route handler is the function a router calls when a route matches. It produces the information needed to render that destination. The handler is not the router: matching, replacing the current view, and updating browser cues remain separate responsibilities. section 10.0 section 11.4.2

Keep the return value small and predictable. In a simple router, each handler returns a title and a view that is a DOM Node. The router places that node in an outlet, updates the document title, marks active links, moves focus, and handles scroll where appropriate. section 11.1 section 11.6

A small handler contract

A route declared with addRoute(path, handler) can associate “/cart” with a handler returning { title: "Cart", view: cartNode }, where cartNode is a DOM Node. The handler supplies the result; the router decides when and how to display it. section 11.4.2 section 10.4.3

Return nodes rather than unsafe HTML strings. If a route loads data, account for cancellation and pending or error states, and distinguish a failed route from an unknown one. A small router need not add loaders, guards, or nested routes unless the application requires them. section 11.6 section 11.4.2

How Vanilla Web covers it

How deep it goes
Defines a small title-and-DOM-node return contract and separates handler results from rendering orchestration; leaves advanced routing features optional.
Examples
Uses addRoute(path, handler) and a router whose handlers return an object containing a title and a Node view.
What it assumes
Defines route handler, view, and page component terminology before using them in a router without requiring a framework.
What you can do after
Build it from scratch with a small handler contract while keeping route resolution, outlet replacement, and browser updates separate.
How it connects
Connects route matching to DOM rendering, Web Components, document metadata, focus management, and scroll handling.

Also explained in

Compare how 2 books explain route handler →

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.