router.routes
Route table containing route functions or lifecycle route objects; the longest matching own path prefix wins regardless of insertion order.
- Returns
- Configuration property.
- Runtime
- Browser + Node.js
URL to application intent
Use one route contract across browser and server runtimes while preserving real links, scoped progressive enhancement, URL semantics, History API behavior, and requestable server routes.
import Router from 'white-label-router';const router = new Router();Routes match complete path boundaries. /products matches /products and /products/42 but not /products-old. When more than one own route prefix matches, Router selects the longest matching prefix so the most specific route wins regardless of declaration order.
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. Router pairs a successfully initialized object-view lifecycle with the exact destroy callback that owned it, even if route configuration is later replaced or mutated. defaultRoute runs when no configured prefix matches.
router.routes = {
'/products': productsRoute,
'/products/sale': saleRoute,
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.
By default opted-in clicks are observed across document. Set navigationRoot to a Document or Element to scope interception to an owned region; reassigning it while active moves the click listener, and null restores document-wide behavior. navigationRoot is separate from callback scope.
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.
const router = new Router();
router.navigationRoot = document.querySelector('[data-product-refinements]');
router.initialize();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 navigation metadata or router:navigate CustomEvent.detail.
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'}
}
}Router validates the destination guard and runnable view behavior before tearing down the current route. 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.
Rejected navigation preserves the last successful URL, route/location state, current route lifecycle, and browser history. 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.
Pass the incoming request URL explicitly to Router in a serverless function. The server path uses the same route matching, query parsing, guards, and lifecycle contract without requiring window or document.
Scope mutable Router state to the request unless sharing is deliberate. Router handles application routing inside an invocation; it does not replace API Gateway, platform routes, CDN rules, or other cloud request routing.
const router = new Router();
router.routes = routes;
router.navigate(new URL(request.url).pathname);router.routesRoute table containing route functions or lifecycle route objects; the longest matching own path prefix wins regardless of insertion order.
router.scopeApplication Element passed to callbacks; null is valid on server paths.
router.navigationRootOptional Document or Element that scopes data-pushstate click interception; null uses document. Changing it while listeners are active moves the owned click listener.
router.mediatorOptional EventTarget-compatible source for router:navigate CustomEvents; an active subscription moves from the previous mediator when this property changes.
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 default fallback behavior 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?)Atomically match a route, enforce its guard/runnable view contract, 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 and Document/Element navigation roots; 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.
Malformed navigation candidates are rejected with false before Router state changes. Server adapters should preserve raw request-target path semantics and hand Router a fully qualified URL.
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. navigationRoot limits interception ownership; callback scope remains application data.