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

@jkempema/web-router

v1.0.7

Published

web-router

Downloads

13

Readme

web-router

route-history

route-history enhances the browser's history api by...

  1. allowing route data to be matched to the pathname in the url
  2. having route changes run through a series of middleware functions that can cancel or redirect
  3. keeping a list of all route changes
  4. allowing other components to listen to when routes are in the process of changing and when they have changed

applyRoutes

The applyRoutes method allows the router to associate the url with some sort of route data. The route data can be pretty much anything, but there are a few key properties.

pattern

This value is matched with location.pathname to associate the current url with the route data. Parameters can specified by :paramName. Optional parts can be surrounded by parentheses.

/some-route
/some-route-with-param/:id
/some-route-with-optional(/:id)

title

If this value is defined, the router will use it to set the document's title property.

applyMiddleware

A middleware function will have an action, nextRoute, and prevRoute arguments. To continue the route change call action.ok(). To abort the route change call action.cancel(). To redirect the route change, call action.redirect( '/some-other-url' ). All three methods take an optional state parameter that can be used to pass additional data.

import { routeHistory } from 'route-history';

const middleware = [
    // Redirect Root to Login or Home
    ( action, nextRoute, prevRoute ) => {
        if ( nextRoute.pathname === '' || nextRoute.pathname === '/' ) {
            if ( authenticated() ) { 
                action.redirect( '/home' );
            } else {
                action.redirect( '/login' );
            }
            
        } else {
            action.ok();
        }
    }, 

    // Redirect to Login When Not Authenticated
    ( action, nextRoute, prevRoute ) => {
        if ( !authenticated() && nextRoute.match && !nextRoute.match.allowAnonymous ) {
            action.redirect( '/login', { props: { redirectTo: nextRoute.pathname } } );
        } else {
            action.ok();
        }
    }
];

routeHistory.applyMiddleware( middleware );

push

The push function adds a new location to the route history.

import { routeHistory } from 'route-history';

routeHistory.push( '/new-location', { props: { someProp: 'someValue' } } ).then( () => {
    // this executes after the route change has been processed.
} );

replace

The replace function replaces the existing location in the route history.

import { routeHistory } from 'route-history';

routeHistory.replace( '/new-location', { props: { someProp: 'someValue' } } ).then( () => {
    // this executes after the route change has been processed.
} );

back

The back function goes back one location in the route history.

import { routeHistory } from 'route-history';

routeHistory.back().then( () => {
    // this executes after the route change has been processed.
} );

go

The go function goes forward or back some delta in the route history.

import { routeHistory } from 'route-history';

routeHistory.go( -2 ).then( () => {
    // this executes after the route change has been processed.
} );

refresh

The refresh function executes any middleware against the current location.

import { routeHistory } from 'route-history';

routeHistory.refresh().then( () => {
    // this executes after the route refresh has been processed.
} );

list

The list function returns an array of all the route location history.

import { routeHistory } from 'route-history';

const routeHistoryList = routeHistory.list();

addEventListener

The addEventListener method allows another chunk of code to listen to when routes are changing or have changed.

import { routeHistory } from 'route-history';

routeHistory.addEventListener( 'changing', () => {
    // this executes at the beginning of a route change.
} );

routeHistory.addEventListener( 'changed', ( action, nextRoute, prevRoute ) => {
    // this executes at the beginning of a route change.
    // action can be "none", "forward", or "back"
} );

removeEventListener

The removeEventListener method allows another chunk of code to stop listening to route changes.

import { routeHistory } from 'route-history';

const unlisten = routeHistory.addEventListener( 'changing', () => {
    // this executes at the beginning of a route change.
} );

unlisten();

const handleChanged = ( action, nextRoute, prevRoute ) => {
    // this executes at the beginning of a route change.
    // action can be "none", "forward", or "back"
} );

routeHistory.addEventListener( 'changed', handleChanged );

routeHistory.removeEventListener( 'changed', handleChanged );