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

flamethrower-router

v0.0.0-meme.12

Published

Blazingly fast SPA-like router for static sites

Downloads

307

Readme

Flamethrower 🔥

Status: Meme

A 2kB zero-config router and prefetcher that makes a static site feel like a blazingly fast SPA.

Why?

Problem: Static sites feel slow and cannot easily share state between pages. This makes it difficult to create a pleasant user experience (UX) with JavaScript libraries because each new page needs to reboot your JS from scratch.

Rather than requiring a frontend framework to take control of the entire DOM, the goal is to make route changes on static sites feel faster, like a SPA.

How?

  1. It tells the browser to prefetch visible links in the current page with IntersectionObserver.
  2. Intercepts click and popstate events, then updates the HTML5 history on route changes.
  3. Uses fetch to get the next page, swaps the <body> out, merges the <head>, but does not re-execute head scripts (unless asked to).

This means you can have long-lived JavaScript behaviors between navigations. It works especially well with native web components.

QuickStart

npm i flamethrower-router
import flamethrower from 'flamethrower-router';
const router = flamethrower();

That's it. Your site now feels blazingly fast.

Advanced Usage

// with opts
const router = flamethrower({ prefetch: 'visible', log: false, pageTransitions: false });

// Navigate manually
router.go('/somewhere');
router.back();
router.forward();

// Listen to events
window.addEventListener('flamethrower:router:fetch', showLoader);
window.addEventListener('flamethrower:router:fetch-progress', updateProgressBar);
window.addEventListener('flamethrower:router:end', hideLoader);

// Disable it
router.enabled = false;

Opt-out of specific links for full page load.

<a href="/somewhere" data-cold></a>

Scripts in <body> will run on every page change, but you can force scripts in the <head> to run:

<script src="..." data-reload></script>

The fetch-progress event is a custom event, so usage will look something like this:

window.addEventListener('flamethrower:router:fetch-progress', ({ detail }) => {
	const progressBar = document.getElementById('progress-bar');
	// progress & length will be 0 if there is no Content-Length header
	const bytesReceived = detail.received; // number
	const length = detail.length; // number
	progressBar.style.width = detail.progress + '%';
});

Prefetching

Prefecthing is disabled by default.

  • visible: prefetch visible links on the page with IntersectionObserver
  • hover: prefetch links on hover
const router = flamethrower({ prefetch: 'visible' });

Misc

Supported in all browsers? Yes. It will fallback to standard navigation if window.history does not exist.

Does it work with Next.js? No, any framework that fully hydrates to an SPA does not need this - you already have a client-side router.

Does it work with Astro? I think so. It can share state between routes, but partially hydrated components may flash between routes.

Other things to know:

  • <head> scripts run only on the first page load. <body> scripts will still run on every page change (by design).
  • It's a good idea to show a global loading bar in case of a slow page load.
  • This library is inspired by Turbo Drive.
  • This project is experimental.

Contributing

Build it:

npm run dev

Serve the example:

npm run serve

Make sure all playwright tests pass before submitting new features.

npm run test

Deploying

You can deploy Flamethrower to Vercel as follows:

npm run deploy

This uses the Build Output API and the Vercel CLI to deploy the /example folder.