Giter Club home page Giter Club logo

flamethrower's Introduction

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' });

Working with url queries

Getting queries

You can also get router query

const router = flamethrower();

// getting query from '/query?foo=bar&id=4'
const myQueries = router.query;
console.log(myQueries); // outputs { foo: 'bar', id: 4 }

Passing queries

You can pass query to url to navigate to

const router = flamethrower();

// navigating to '/query?foo=bar&id=4'
router.go('/query', {
  foo: 'bar',
  id: 4,
});

NOTE You don't need to call router.go(...) when you used anchor tags in html. Flamethrower will automatically detect url and queries

<a href="/query?foo=bar&id=4">Query Page</a>

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.

flamethrower's People

Contributors

codediodeio avatar ekwoka avatar b0iq avatar omar-dulaimi avatar prowebat avatar tsar-boomba avatar vinaykulk621 avatar sunneydev avatar 7flash avatar badhan-abhishek avatar xeospheric avatar ndungtse avatar sb3p avatar alonhor avatar emmacyril avatar dhirajgagrai avatar dezren39 avatar ishankbg avatar leerob avatar zeepk avatar oumarbarry avatar oriyadid avatar rodrigoteran avatar sebastianopperman avatar tobyleye avatar amirkian007 avatar depapp avatar okyanusoz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.