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

nicholas-router

v1.0.5

Published

Nicholas is a small, extend-able client-side caching, and routing layer for websites. It was originally built to make using the [Nearly Headless](https://www.wpdev.academy/concepts/headless-wordpress-is-overrated-a-case-for-the-nearly-headless-web-app/) a

Downloads

4

Readme

Nicholas

Nicholas is a small, extend-able client-side caching, and routing layer for websites. It was originally built to make using the Nearly Headless approach easier to accomplish, but it is also most-likely compatible with fully headless sites.

What Nicholas Does

Out of the box, Nicholas does very little. It is up to you to put together the actions that control when, and how Nicholas caches, and routes. It accomplishes this using a middleware patterns that determine:

  1. How Nicholas is set up
  2. What it does when a cached item is clicked
  3. What gets saved to the cache

Nicholas also has some pre-built middlewares that can be used to make setting up the cache engine easier.

Installation

npm install nicholas

Setup

To set up Nicholas, you must run setupRouter() sometime after your content has loaded. A common way to-do this is like so:

import { setupRouter } from 'nicholas';

window.onload = () => setupRouter()

setupRouter uses a middleware pattern, so if you need to expand on what happens when the router is set up, you can do so by passing middleware callbacks, much like how Express handles routing middleware:

import { setupRouter } from 'nicholas';

window.onload = () => setupRouter(
	( args, next ) => {
		// Do things
		next()
	}
)

Routing

A link can be routed manually at any time using route function.

import { route } from 'nicholas'

route( { url: 'https://www.url-to-route-to.com' } )

It can also be added as an event listener as a middleware inside setupRouter.

import { setupRouter, route } from 'nicholas';

window.onload = () => setupRouter( ( args, next ) => {
	// Set up click listener
	document.addEventListener( 'click', ( event ) => route( { event } ) );
	next()
} )

Extending Route Actions

When the route function is called, Nicholas runs a set of actions in the order in-which they are added. You can add actions that should occur when a URL is being routed at any time with addRouteActions. You can add as many actions as you need.

Nicholas does not do anything with the history. This is because it doesn't know how to handle browser history. Instead, Nicholas assumes that you'll set up history inside addRouteActions through the context of your app.

import { setupRouter, addRouteActions } from 'nicholas';

// When a URL is visited, do these actions
addRouteActions( ( { event, url }, next ) => {
	// Stop the event from doing what it would normally do
	event.preventDefault();

	// Access the cached data for the current url
	const cache = args.url.getCache()

	// Now do things with the cached data!

	// Move on to the next action.
	next()
} )

// Setup
window.onload = () => setupRouter()

You can also stop a route from using nicholas at any time with a return call in your function, instead of running next(). This allows you to add further validations within the context of your app.

import { setupRouter, addRouteActions } from 'nicholas';

// When a URL is visited, do these actions
addRouteActions( ( { event, url }, next ) => {
	return
} )

// Setup
window.onload = () => setupRouter()

Extending What Saves to The Cache

When cacheItems runs, it loops through a set of actions in the order in-which they were added. You can add actions that save data for the found URLs to the cache using addCacheActions. You can add as many actions as you need.

addCacheActions( ( { urls }, next ) => {
	// Loop through the found URLs.
	urls.forEach( url => {
		// Get the data that needs saved to the cache. This could be an API call, or something else.
		const data = {};

		// Update the cache for the URL and save the data
		url.updateCache( data )
	} )

	// Move on to the next action
	next()
} )

Manipulating The Cache Directly

To save to the cache, use the URL object.

import { Url } from 'nicholas'

// Construct a URL object from the current page. You can replace this with any local URL
const currentUrl = new Url( window.location.href )

// Merge this data with the existing cache data.
currentUrl.updateCache( { custom: 'data to add to this page cache' } )

// This will get the cache data
const cachedData = currentUrl.getCache()

It is important to note that updateCache does not replace the cache data, it merges it with what is already in the cache. If you want to completely replace the cache data, you need to clear the cache first.

import { Url } from 'nicholas'

// Construct a URL object from the current page. You can replace this with any local URL
const currentUrl = new Url( window.location.href )

// First, clear the cache for this URL
currentUrl.clearCache()

// Set up the object
currentUrl.updateCache( { custom: 'data to add to this page cache' } )

// Get the cached data
const cachedData = currentUrl.getCache()

Clearing All Cached Data

The Url object includes a way to clear the cache for a specific URL, but what happens if you want to clear all cached data? This can be accomplished using the clearCache function, like so:

import { clearCache } from 'nicholas'

// Clears ALL data cached by Nicholas.
clearCache()

Send a Signal to Clear a Session's Cache

If the clearSessionCacheMiddleware seup middleware is used, it's possible to clear the session's cache automatically. This can be accomplished by setting a nicholas_flush_cache cookie. This middleware will automatically delete the cookie, and wipe the cache.

This is useful in scenarios where the user's session changes, and cached data is invalidated.