npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

steroid-router

v1.0.4

Published

A simple client side router for vanilla JS applications.

Downloads

159

Readme

steroid-router

npm license

A simple client side router for vanilla JS applications.

Below link has the barebone implementation of client side routing using steroid-router https://steroid-router.surge.sh

Install

$ npm install steroid-router

Usage

As ES6 Module with bundlers like webpack, etc.

import router from 'steroid-router'

// initialize route handlers
// support all kind of express js like route syntax
router(<routes>, <options>);

As legacy javascript files using script tags.

<script src="https://cdn.jsdelivr.net/npm/steroid-router@latest/dist/steroid-router.min.js"></script>
...
<script>
router(<routes>, <options>);
</script>

Parameter

  • routes - array of route object.
  • options (optional) - options object (refer https://www.npmjs.com/package/path-to-regexp#usage for all available options).

For the given location - example.com/course/demo


function courseRoute (data) {
 console.log(data.params.courseName);
 // demo
 console.log(JSON.stringify(data));
 // {"matched":["/course/demo/","demo"],"params":{"courseName":"demo"}, "state": {}}
 // further route
}
....
// other route handlers
....

let routeHandler = router([{
    match: '/course/:courseName?', // route with optional courseName route parameter
    action: courseRoute
  }, {
    match: '/merchandise/:category', // route with mandatory category route parameter
    action: merchandiseRoute
  }, {
    match: '/', // a fixed route
    action: indexRoute
  }, {
    match: '(.*)', // fallback to catch all non-matched routes - used for 404 redirects
    action: routeNotFound
  }]);

// Return object with function name navigateTo
  • match - matches the given route
  • action - a function called with route match data when route match occurs

Route match data

 {"matched":["/course/demo/","demo"],"params":{"courseName":"demo"}, "state": {}}
  • matched - matched path regex array. Eg: ["/course/demo/","demo"]
  • params - params object of the route. if any route parameter is specified. Eg: {"courseName":"demo"}
  • state - state object of the route. if any state is passed from the previous route

Note: Always first matching route will be used.

Navigation

steroid-router supports both declarative and imperative navigation approach

Declarative navigation

Just add the class steroid-route to the anchor tag <a> to make it as steroid route.

<a class="steroid-route" href="/dashboard">Dashboard</a>

where, href if the route path.

Additonally, optinal navigation parameters can be passed using data-* attributes like below:

<a class="steroid-route" href="/course" data-withstack="true" data-withstate='{"id":2}' data-withtitle="course" data-withaction="true">Course</a>

Imperative navigation

Router function returns object with navigation menthod named navigateTo()

routeHandler.navigateTo(url, withStack?, state?, title?, action?);
  • url - the client side route to navigate to
  • withStack (optional) - whether the current route route need to be stacked. default: false
  • state (optional) - state object to pass to the route
  • title (optional) - Title of the route - not widely adopted (Refer: https://github.com/whatwg/html/issues/2174 for more details)
  • action (optional) - Whether route action should be triggered - useful for post navigation url updates. default: true

Server

Make sure your backend server is configured to serve the respective html files where the routes are defined.

For more detailed information about different types of routing refer the article below: https://johnpremkumar.medium.com/routing-a-delusion-among-developers-9c34e6fddd47

Used By

License

MIT