- API receives array instead of object for match order - no longer supports unmatched function (just put a catchall route at the end)
Router
The core functions of router that is designed to be slim and have an easy-to-read, yet powerful API.
NOTE This router is geared toward offline-first apps and thus does not support pushState.
Usage
Instantiation
const router = Router(routeSpecArray, rootURL='#'):
routeSpecArray
An array of objects with the following properties:
-
namestring - an optional string that can be referred to in thehrefandgotoinstance methods. Duplicate names are not allowed. -
pathstring - the path template for this route. Path templates are matched in the order of the array. Example:"/"- for the root path"/articles"- another static path"/article/:id"- a path with a variable"/:unknownRoute"- the last route could catch erroneous routes. Unmatched urls will automatically route here. -
varsobject - an optional object mapping variable names in the path template to a regular expression for validation -
enterfunction - a function for when this route is entered. Theenterfunction receives two parameters:-
route instance object - this is a object that contains properties:
namestring - the route namevarsobject - an object holding any variables parsed from the pathpathstring - the path as received
-
router instance object - (see below)
The
enterfunction may return a callback that will be called instead of theenterfunction for further navigate events that will be handled by this route (with different variables). This allowsenterto establish a context for the route it handles. -
-
exitfunction - an optional function that will be called before callingenterof the next path.exithas the option to delay the call toenterby returning a promise. This is intended for handling transition animations. If the route'senterfunction returns a callback,exitwill not be called if the same route receives navigation but with different variables.exitreceives the parameters similarly toenter:- route instance object - for the route being exited
- route instance object - for the route yet-to-be entered
- router instance object - (see below)
rootURL (optional string)
The url prefix of all paths. This should always be # unless you're nesting routers.
router (returned object)
The returned instance provides these methods:
-
goto(url: string)orgoto(pathName: string, vars: object)Match to a route by relative url or pathName and a vars object. Navigate there.
-
href(pathName: string, vars: object)Build a relative url based on the name and supplied vars.
-
start(initialRoute: string)Listen to
window.onhashchangefor route changes. TheinitialRoutewill be passed togoto()if the is no current route inwindow.location. -
stop()Cancel subscription to
window.onhashchange -
current()Get the current route instance object as was provided to the current routes
enterfunction.
Example:
const router = Router([
{
name: 'home',
path: '/',
enter: (r, route) => {
console.log('At application home');
},
exit: (r, route, newRoute) => {
console.log(`Exiting from ${route.path} to ${newRoute.path}`);
}
},
{
name: 'article',
path: '/article/:id',
vars: { id: /[a-f0-9]{6,40}/ },
enter: (route, router) => {
console.log('Opening Article', route.vars.id);
return (route, router) => {
console.log('Opening Article', route.vars.id);
};
},
exit: (route, newRoute, router) => {
console.log('Closing Article', route.vars.id);
}
},
{
id: '404',
path: ':vars',
enter: r => r.goto('home')
}
]);
router.start('/');