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

lightweight-router

v1.0.2

Published

A lightweight client-side router with prefetching capabilities.

Downloads

161

Readme

Lightweight Router

A minimal lightweight client-side router with intelligent prefetching capabilities for faster websites. This tool can turn any Multi-Page Application (MPA) into a Single-Page Application (SPA) very easily and with just ~1.5KB byte (gzipped).

Features

  • 🚀 Zero dependencies
  • 🔄 Smooth client-side navigation
  • 📥 Intelligent link prefetching
  • 🎯 Multiple prefetching strategies
  • 🔍 SEO-friendly (works with Wordpress)
  • 📱 Mobile-friendly with data-saver mode support
  • 🎨 Built-in loading animations
  • 🕰️ Based on History API so you can use native browser navigation

Installation

NPM

npm install lightweight-router

Usage

To use the lightweight router in your project, follow these steps:

  1. Import the startRouter function from the router module.
  2. Call the startRouter function to initialize the router.

Example:

import { startRouter } from "lightweight-router";

startRouter({
  onRouteChange: currentRoute => {
    console.log("Route changed:", currentRoute);
  },
});

Direct Import

You can also directly import the minified version of the router in your HTML file or paste its content inside a script tag:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>My App</title>
  </head>
  <body>
    <!-- Your website content -->
    <script src="path/to/dist/router.min.js"></script>
  </body>
</html>

API

startRouter(options)

Initializes the router with the given options.

Parameters

  • options (Object): Configuration options for the router.
    • onRouteChange (Function): Callback function to be called when the route changes.

Examples

Basic Example

Your website content
<script type="module">
  import { startRouter } from "./router.js";

  startRouter({
    onRouteChange: currentRoute => {
      console.log("Route changed:", currentRoute);
    },
  });
</script>

Server Configuration

Configuring your server to return only the route content can make the router much more efficient. Instead of returning the entire page, the server should return only the content for the requested route when it detects a request with the message "onlyRoute".

await fetch(url, { method: "POST", body: "onlyRoute" });

This allows only the changing part of the document to be updated, improving performance and reducing bandwidth usage.

Once you configured your server to respond to this type of request, wrap the part of your document that changes in a router tag. Inside the router tag, render the current initial route inside a route tag like this:

<-- Header menu and parts that don't change -->
<router>
  <route path="/" style="content-visibility: auto">home content</route>
</router>
<-- footer etc.. -->

You can also prerender other important routes by rendering them inside the router tag in their appropriate route tags for faster loading times:

<router>
  <route path="/" style="content-visibility: auto">home content</route>
  <route path="/about" style="content-visibility: auto; display:none;">about content</route>
</router>

In the future you will also be able to pre-render a default route that will be used as 404 by having it at /404 or /default

Right now errors are shown without styling as the content of the page.

Soon there will be a DenoJS library that will help you deal with all these routes stuff. It will also come with api routes functionality 🔥

Prefetching

By default, links are prefetched when they get in the user's screen using an IntersectionObserver. This ensures that the content is loaded in the background before the user clicks on the link, providing a smoother navigation experience.

If you have too many links at once or too many requests, you can add the prefetch="onHover" attribute to your links or some of them (usually links to huge pages that are not often visited):

<a href="/archive" prefetch="onHover">Archive</a>

P.S. you can easily test in your website by pasting this minified version into the console.

The minified version was created with uglify-js, clean.css and then minified again with https://packjs.com The size of the gzipped version was calculated with: https://dafrok.github.io/gzip-size-online/ It's worth to note that nonethewise Terser give better results than uglify-js. The final uglify version packed by packjs.com was even smaller.

Browser Support

The router supports all modern browsers. Required features:

  • IntersectionObserver
  • Fetch API
  • History API

For older browsers, consider using the following polyfills:

  • intersection-observer
  • whatwg-fetch

Performance Tips

  • Use content-visibility: auto on route elements to improve rendering performance
  • Implement server-side partial responses for better bandwidth usage
  • Consider using the prefetch="onHover" attribute for less important links