Flamethrower

A blazingly fast router for static sites

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 `` out, merges the ``, 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


  1. ```
  2. npm i flamethrower-router
  3. ```

  1. ``` js
  2. import flamethrower from 'flamethrower-router';
  3. const router = flamethrower();
  4. ```

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

Advanced Usage


  1. ``` js
  2. // with opts
  3. const router = flamethrower({ prefetch: 'visible', log: false, pageTransitions: false });

  4. // Navigate manually
  5. router.go('/somewhere');
  6. router.back();
  7. router.forward();

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

  12. // Disable it
  13. router.enabled = false;
  14. ```

Opt-out of specific links for full page load.

  1. ``` html
  2. <a href="/somewhere" data-cold></a>
  3. ```

Scripts in `` will run on every page change, but you can force scripts in the `` to run:

  1. ``` html
  2. <script src="..." data-reload></script>
  3. ```

The fetch-progress event is a custom event, so usage will look something like this:
  1. ``` js
  2. window.addEventListener('flamethrower:router:fetch-progress', ({ detail }) => {
  3. const progressBar = document.getElementById('progress-bar');
  4. // progress & length will be 0 if there is no Content-Length header
  5. const bytesReceived = detail.received; // number
  6. const length = detail.length; // number
  7. progressBar.style.width = detail.progress + '%';
  8. });
  9. ```

Prefetching


Prefecthing is disabled by default.

- visible: prefetch visible links on the page with IntersectionObserver
- hover: prefetch links on hover

  1. ``` js
  2. const router = flamethrower({ prefetch: 'visible' });
  3. ```

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:

- `` scripts run only on the first page load. `` 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:

  1. ```
  2. npm run dev
  3. ```

Serve the example:

  1. ```
  2. npm run serve
  3. ```

Make sure all playwright tests pass before submitting new features.

  1. ```
  2. npm run test
  3. ```

Deploying


You can deploy Flamethrower to Vercel as follows:

  1. ```
  2. npm run deploy
  3. ```

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