Programming language: JavaScript
License: MIT License
Tags: Routing    
Latest version: v1.2.0

navaid alternatives and similar libraries

Based on the "Routing" category.
Alternatively, view navaid alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of navaid or a related project?

Add another 'Routing' Library


A navigation aid (aka, router) for the browser in 865 bytes~!


$ npm install --save navaid


const navaid = require('navaid');

// Setup router
let router = navaid();
// or: new Navaid();

// Attach routes
  .on('/', () => {
    console.log('~> /');
  .on('/users/:username', params => {
    console.log('~> /users/:username', params);
  .on('/books/*', params => {
    console.log('~> /books/*', params);

// Run as single instance
//=> "~> /"
//=> "~> /users/:username { username: 'lukeed' }"
//=> "~> /books/* { wild: 'kids/narnia' }"

// Run as long-lived router w/ history & "<a>" bindings
// Also immediately calls `run()` handler for current location


navaid(base, on404)

Returns: Navaid


Type: String Default: '/'

The base pathname for your application. By default, Navaid assumes it's operating on the root.

All routes will be processed relative to this path (see on()). Similarly, while listening, Navaid will not intercept any links that don't lead with this base pathname.

Note: Navaid will accept a base with or without a leading and/or trailing slash, as all cases are handled identically.


Type: Function Default: undefined

The function to run if the incoming pathname did not match any of your defined defined patterns. When defined, this function will receive the formatted uri as its only parameter; see format().

Important: Navaid only processes routes that match your base path! Put differently, on404 will never run for URLs that do not begin with base. This allows you to mount multiple Navaid instances on the same page, each with different base paths. :wink:


Returns: String or false

Formats and returns a pathname relative to the base path.

If the uri does not begin with the base, then false will be returned instead. Otherwise, the return value will always lead with a slash (/).

Note: This is called automatically within the listen() and run() methods.


Type: String

The path to format.

Note: Much like base, paths with or without leading and trailing slashes are handled identically.

route(uri, replace)

Returns: undefined

Programmatically route to a path whilst modifying & using the history events.


Type: String

The desired path to navigate.

Important: Navaid will prefix your uri with the base, if/when defined.


Type: Boolean Default: false

If true, then history.replaceState will be used. Otherwise history.pushState will be used.

This is generally used for redirects; for example, redirecting a user away from a login page if they're already logged in.

on(pattern, handler)

Returns: Navaid

Define a function handler to run when a route matches the given pattern string.


Type: String

The supported pattern types are:

  • static (/users)
  • named parameters (/users/:id)
  • nested parameters (/users/:id/books/:title)
  • optional parameters (/users/:id?/books/:title?)
  • any match / wildcards (/users/*)

Note: It does not matter if your pattern begins with a / — it will be added if missing.


Type: Function

The function to run when its pattern is matched.

Note: This can also be a Promise or async function, but it's up to you to handle its result/error.

For patterns with named parameters and/or wildcards, the handler will receive an "params" object containing the parsed values.

When wildcards are used, the "params" object will fill a wild key with the URL segment(s) that match from that point & onward.

let r = new Navaid();

r.on('/users/:id', console.log).run('/users/lukeed');
//=> { id: 'lukeed' }

r.on('/users/:id/books/:title?', console.log).run('/users/lukeed/books/narnia');
//=> { id: 'lukeed', title: 'narnia' }

//=> { id: 'lukeed', wild: 'foo/bar/baz/bat' }


Returns: Navaid

Executes the matching handler for the specified path.

Unlike route(), this does not pass through the history state nor emit any events.

Note: You'll generally want to use listen() instead, but run() may be useful in Node.js/SSR contexts.


Type: String Default: location.pathname

The pathname to process. Its matching handler (as defined by on()) will be executed.


Returns: Navaid

Attaches global listeners to synchronize your router with URL changes, which allows Navaid to respond consistently to your browser's BACK and FORWARD buttons.

These are the (global) events that Navaid creates and/or responds to:

  • popstate
  • replacestate
  • pushstate

Navaid will also bind to any click event(s) on anchor tags (<a href="" />) so long as the link has a valid href that matches the base path. Navaid will not intercept links that have any target attribute or if the link was clicked with a special modifier (eg; ALT, SHIFT, CMD, or CTRL).


Type: String Default: undefined

(Optional) Any value passed to listen() will be forwarded to the underlying run() call.When not defined, run() will read the current location.pathname value.


Returns: undefined

Detach all listeners initialized by listen().

Note: This method is only available after listen() has been invoked.


MIT © Luke Edwards

*Note that all licence references and agreements mentioned in the navaid README section above are relevant to that project's source code only.