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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@nxcode-npm/vanillajs-router

v1.0.2

Published

VanillaJS-router is a lightweight module written in pure Javascript, with no dependencies, for rapid development of Single Page Apps. Based on standard Web Component. Provides page-level state management, document fragments and nested routes, and uses web

Readme

vannillaJS-router

VanillaJS-router is a lightweight module written in pure Javascript, with no dependencies, for rapid development of Single Page Apps. Based on standard Web Component. Provides page-level state management, document fragments and nested routes, and uses web history APIs to simulate browser navigation.


// window global object
RO.initialize(my_app_router)
RO.nav({key:"myview"})

// OR
import { RO } from "..."

RO.initialize(my_app_router)
RO.nav({key:"myview"})

App router

Where all the app routing is defined.

const app_router:IRouter = {
   'index':{
       customElement:'index-page',
       params:{},
       router:() => document.querySelector("router-component#app")
   },
   'home':{
       customElement:'home-page',
       params:{},
       router:() => querySelector("router-component#app")
   },
   
}

index.page.ts

class IndexPage extends HTMLElement {

   static route:string = "index"

   constructor(){
       super()
   }

   connectedCallback(){
       this.innerHTML = `<p>This is the index page</p>
           <a href="#" onclick="RO.nav({key:'home'}); return false;">Go Home</a>`
   }

}

customElements.define('index-page', IndexPage)

home.page.ts

class HomePage extends HTMLElement {

   static route:string = "home"

   constructor(){
       super()
   }

   connectedCallback(){
       this.innerHTML = `<p>This is the home page</p>
           <a href="#" onclick="RO.nav({key:'index'}); return false;">Logout</a>`
   }

}

customElements.define('home-page', HomePage)

index.html

<!doctype html>
<html lang="en">
   <head>
       <meta charset="utf-8">
       <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
       <meta name="Description" content="">
       <base href="/">
       <title>Test vanillajs-router</title>
   </head>
   <body>
       <router-component id="app" route="index"></router-component>
   </body>
</html>

App startup

RO.initialize(app_router)

Params

// "index" passes a message to "home"
// [index.page.ts]
RO.nav({message:"Hi, happy to see you"})

// "home" reads the message"
// [home.page.ts]
constructor(){
    super()
    this.route = app_router[HomePage.route]
    this.message = route.params.message
}

Nested routes

The "router()" parameter in "app router" is a function that returns a parent RouterComponent, which is where the view is inserted into the DOM. This mechanism allows you to load some parts of the page and leave others fixed (for example the main navigation bar).
The following example shows how to add nested routes to the "home" page. For example "home account" loads the view in the "route-component#home" and adds the home path in the browser URL (//myserver/home/account) to the page name.

 enum paths { HOME="home" }
 const getHomeRoot => (){
    return document.querySelector(`router-component#home`)
 }

 const app_router:IRouter = {
    //...
    'home_account':{
        customElement:'home-account',
        path:paths.HOME.concat('/'),
        name:'account',
        params:{},
        routerLink:()=> getHomeRoot()
    },
    'home_projects':{
        customElement:'home-projects',
        path:paths.HOME.concat('/'),
        name:'projects',
        params:{},
        routerLink:()=> getHomeRoot()
    },
    'home_messages':{
        customElement:'home-messages',
        path:paths.HOME.concat('/'),
        name:'messages',
        params:{},
        routerLink:()=> getHomeRoot()
    }
    //...
 }

home.html

<!--  
  Navigating to "home_message" will not update the header   
  -->
<rounter-component id="app" route="home">
       <header>
           <h1>User Home</h1>
       </header>
      <router-component id="home" route="home_account"></router-link>
</router-component>

Page state

Save a page state in the route "state" prop; the state of a page can be retrieved from its route when it is reloaded or following user back/forward navigation.

// save the state
app_router["home_account"].state["user"] = { user }

// read the state
connectedCallback(){
   this.user = app_router[HomeAccountPage.route].state["user"]
}