npm install --save riot-tagrouter
Riot js comes with a pretty rock solid router along with the library file. This router however is purely imperitive and maintaining it in a large codebase can go out of hand pretty easily.
riot-tagrouter is a declarative wrapper around the same, it uses the same riot router in its core and auto starts the riot router upon its mount.
This router aims to expose a more simpler and declarative way to Router for riotjs apps that not only can work super easily out of the box, but also has first class support for lazy loading of the routes and PRPL for app.
The API is kept pretty clean and minimal, a minimal router config would be declared as follows:
in the above example base-route will be the route base customizable as per the riot router, this sets the root url for your entire app.
This router as promised also has first class support for lazy loaded routes again in a declarative way. The component attribute of the router takes one of the two parameter
- A string type name of the component to be mounted
- A function which either returns a string or a promise which at the end needs to be resolved with a string type name of the component
see Lazy Loading your Riotjs SPA for hints on webpack stuff
Custom navigation element
element bundled with this package uses riot.route internally to navigate to any route, for a11y purpose all navigate elements will be '' tags.
Route values to components
The component specified in the route tag(or passed via promise) will recieve all the route params in opts of the component.
e.g. in above example, path '/user/:user' the component 'user-profile' wil recieve an opts 'user' with the route value.
Following two events will be published on 'Router' tag's tag defination(using .trigger) and also via .opts (i.e. 'on-' attributes)
- Route Changed: Whenever the route is changing a 'routeChange' event will be triggered on the tag implementation and also if a function is passed to 'on-routechange' attribute then that will be called too.
- Tag not found: Whenever a while mounting a tag is not found a 'tagNotFound' event will be triggered on the tag implementation and also if a function is passed to 'on-tagnotfound' attribute then that will be called too.
Now obviosly, time to first meaningful paint is the new perf metrics we cannot miss the importance of, hence server rendered streamed HTML is something of utmost importance for riot-tagrouter.
That brings in the need of Isomorphism from the router's side. We aim at making this a super easy process for the users. For server side rendering allow riot.render to inject the your router component into your markup.
var riot = ;var appRoutingTag = ;//console.log or res.render or how ever u wanna do itres;
After NPM installation, inside the node_modules > riot-tagrouter > build will be the routerlib.js(ES5 verison) or router_tags_es6.js(ES6 version), feel free to use the build toold of your choice(Webpack,Grunt, gulp).
Also a raw(Un Rioted and ES6) version of all the tags lie in tags folder if you want a specific loader to precess it for your Webpack.
- All ':slugs' are replaced by '*' internally, you can however use all the rules that you can use in riot router, however url params will be passed as opts only for ':slug' keys.
- It is highly recommended to keep your router instance inside another riot tag when you are planning a lazy loading component, it will help you pass component as a function.