router.routes
Ordered route table containing route functions or lifecycle route objects.
- Returns
- Configuration property.
- Runtime
- Browser + Node.js
URL to application intent
Use one route contract across browser and server runtimes while preserving real links, URL semantics, History API behavior, and requestable server routes.
import Router from 'white-label-router';const router = new Router();Routes are evaluated in insertion order and match complete path boundaries. /products matches /products and /products/42 but not /products-old. Put specific prefixes before broad prefixes.
A route can be a function or an object with title, focus, secure, and view lifecycle fields. secure() must return exactly true to allow navigation. defaultRoute runs when no configured prefix matches.
router.routes = {
'/products/sale': saleRoute,
'/products': productsRoute,
defaultRoute
};Browser navigation should start with a real anchor. data-pushstate opts eligible same-origin links into client-side navigation. Modified clicks, non-left clicks, downloads, alternate targets, and cross-origin URLs stay native.
Initialization dispatches the current URL without creating a duplicate history entry. Back/forward navigation reads the authoritative window.location rather than trusting arbitrary history state.
<a href="/products/42" data-pushstate>View product 42</a>Route callbacks receive scope and a location object. data.url contains decoded path segments after the matched prefix, data.query contains the last value for each query key, and data.mediator contains programmatic/mediator navigation metadata.
Applications remain responsible for validating decoded URL values before using them as trusted domain data.
{
url: '/products/42?color=blue',
data: {
url: ['42'],
query: {color: 'blue'},
mediator: {source: 'featured'}
}
}On a successful route change, the previous route view's destroy() runs before the next view's initialize(). Browser object routes can update document title and focus after rendering.
The default focus selector is main h1. Set focus: false when navigation represents an in-page refinement that should preserve focus. Server runtimes keep pageTitle state but do not mutate a document or focus target.
router.routesOrdered route table containing route functions or lifecycle route objects.
router.scopeApplication Element passed to callbacks; null is valid on server paths.
router.mediatorOptional EventEmitter-compatible source for router:navigate messages.
route.titleDocument title applied after browser routing; retained as pageTitle state on servers.
route.focusSelector to focus after browser navigation; defaults to main h1, false preserves focus.
route.secure(scope, location)Navigation guard. Only an exact true result allows the route.
route.viewRoute function or lifecycle object with initialize() and optional destroy().
new Router()Create an independent router with empty routes and isolated listener state.
initialize(url?)Dispatch the current browser URL or an explicit server URL and attach applicable listeners.
| Parameter | Type | Default | Description |
|---|---|---|---|
url | string | — | Optional in browsers; server applications should normally provide the request URL. |
addListeners()Attach browser and optional mediator handlers once.
destroy()Release routing listeners owned by the instance.
removeListeners()Release listeners owned by this router; subclasses may extend this lifecycle hook.
eventPushStateClick(event)Intercept an eligible same-origin data-pushstate link while preserving native browser actions that should not be enhanced.
eventPopState()Dispatch the authoritative current browser history location without adding a new entry.
parseQueryString(query)Decode query parameters using URLSearchParams and keep the last duplicate value.
setLocationData(data?)Rebuild decoded path/query/mediator payload for the selected route.
applyPageContext(route)Apply configured title and focus after routing when the runtime supports those effects.
navigate(url?, data?, isPopState?)Match a route, enforce its guard, transition view lifecycle, and update browser history when applicable.
| Parameter | Type | Default | Description |
|---|---|---|---|
url | string | — | Target URL; current browser URL when omitted, / fallback outside browsers. |
data | object | — | Caller metadata exposed as location.data.mediator. |
isPopState | boolean | — | Prevent creation of a new history entry for back/forward dispatch. |
Router.Route, Router.Location, Router.Navigation, and Router.Handler expose the supported public route contracts.
DOM types are present because the same class supports browser scopes/events; server hosts do not need DOM globals at runtime.
URL and query values are decoded, not trusted. Validate them before using them for authorization, data access, or domain decisions.
route.secure is an application guard hook; Router does not implement authentication policy.
Router owns URL-to-intent translation. It does not own state, rendering, data loading, authentication policy, or server infrastructure.
Real links and directly requestable server URLs remain the foundation; Router enhances them instead of replacing them.