Skip to content
nav-router

API / Classes

Router class

Holds the route tree, runs each navigation from the navigate event to the rendered page, and keeps the current page. An app has one.

import { Router } from 'nav-router'core 11.3 kB min+br

Create a router

Pass one options object. Keys the router declares are its settings; every other key belongs to the root route, so a small app reads as a single tree.

An unknown key lands on the root route

A key that is neither a setting nor a route option is kept on the root route unchecked. A misspelled setting is not an error, so check router.config when a setting seems ignored.

Settings

Every setting and its default. The resolved values are on router.config.

SettingTypeDefaultDescription
basestring''Path prefix the app is served under.
holdCommitduration'300ms'How long the URL waits for guards and data before committing. A fast load settles before the URL moves.
loadingDelayduration'200ms'Time after commit before the loading view shows.
loadingMinduration'500ms'Once shown, how long the loading view stays, so it never flickers.
statusViewsobject{}Views for loading, notFound, forbidden and error.
signInRoutestringnullWhere a refused guard sends a signed-out user.
errorRoutestringnullWhere a navigation that throws is sent.
identityvalue | functionnullThe user guards read: a value, or a function of the navigation returning one or a promise of one.
restoreScrollstringnullSelector of an inner scroller whose position is restored on back and forward.
attachGlobalstring'router'Name the router is exposed under on window, for the console.
onChangefunctionnullCalled when the page or pending state changes.
onRenderfunctionnullCalled after each render.

See the timing settings run

The trace on the homepage plays one navigation against holdCommit, loadingDelay and loadingMin.

Methods

Each method is also exported as a function that acts on the page’s router, so a view can call navigate() without holding it.

Goes to a route by name, a Route, or a literal path. args is one flat object: keys the route declares as params fill the path, every other key becomes the query.

targetstring | Routerequired

A route name, a Route, or a path such as '/team/acme'.

argsobjectdefault {}

Params and query in one object.

optionsobjectdefault {}

How the entry is written and what the page does after.

8 options
history'auto' | 'push' | 'replace'default 'auto'

Push a new entry, or replace the current one. 'auto' replaces when the URL is unchanged.

stateany

Stored on the history entry.

infoany

Passed to this navigation only.

resetScrollbooleandefault true

Scroll to the top after render.

resetFocusbooleandefault true

Move focus to the page after render.

hashstring

Fragment to append.

keepQuerybooleandefault false

Carry the current query into the new URL.

absolutebooleandefault false

Build the URL with its origin.

{ committed: Promise, finished: Promise }

The Navigation API’s two promises, committed when the URL changes and finished when the page has rendered.

members.js

Without the Navigation API, this is a page load

In a browser without window.navigation, navigate() falls back to location.assign() and the page reloads.

href()

Builds the URL navigate() would go to, without going. Use it for <a href>, so links stay plain anchors.

targetstring | Routerequired

As in navigate().

argsobjectdefault {}

As in navigate().

options{ hash, absolute, keepQuery }default {}

The URL options of navigate().

string

The URL, with the router’s base applied. A path outside the app is returned unchanged.

back()

Goes back one entry. When there is no earlier entry in this app, such as a page opened from a shared link, it navigates to fallback instead.

fallbackstring | Route

Where to go when there is nothing to go back to. The root route when left out.

argsobjectdefault {}

Params and query for fallback.

Properties

PropertyTypeDescription
currentMatchThe committed match: route, params, query and chain.
pagePageWhat is on screen, including its status.
pendingPending | nullThe navigation in flight, with committed and loading flags.
canGoBackbooleanWhether there is an earlier entry in this app.
canGoForwardbooleanWhether there is a later entry.
configobjectThe resolved settings.

Don’t write to these

The properties are read-only views of the router’s state. Change the page with navigate(), or a route’s data with update().

Edit this pageUpdated Nov 5, 2026