vanilla-spa-router

A simple-to-use VanillaJS (SPA) cross-browser router

Usage no npm install needed!

<script type="module">
  import vanillaSpaRouter from 'https://cdn.skypack.dev/vanilla-spa-router';
</script>

README

vanilla-spa-router

Simple-to-use VanillaJS (SPA) Router

Probably you even no need to use this but it's ok :)

Advantages:

  • NO dependencies
  • NO conflicts
  • NO overrides
  • just include as source or module
  • IE6+, Firefox 2+

Installation

... as source <script src="router.js"></script> ... as module yarn add vanilla-spa-router

API

Use window._Router(ops) or new Router(ops) to access to routing

Available options

  • mode: 'history'(default) or 'hash' Choose the way of routing
  • base: '/'(default) Set base directory

Methods

  • .init(ops) Rebind your options accordingly
  • .start() Start working with router using History API or #anchor
  • .use(String route, Function middleware) Subscribe for router updates for exact path with needed params middleware(Object data) - Receives transformed data from path name. You can continue working straight with Router using this inside middleware
  • .navigate(String path) Replaces you to the needed location