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

@jsenv/pwa

v6.1.3

Published

Service worker and other progressive web application helpers

Downloads

29

Readme

pwa npm package

@jsenv/pwa is a tool that can be used to implement the apis required to turn a website into a progressive web application:

  • Add to home screen
  • Service workers

Add to home screen

User can choose to add a shortcut to your website in their device. The navigator will then run your website with only your ui in fullscreen.

The following html displays a button enabled when add to home screen is available. Clicking on the button prompt user to add the website to home screen.

<!doctype html>
<html>
  <head>
    <title>Title</title>
    <meta charset="utf-8" />
    <link rel="icon" href="data:," />
    <script type="importmap">
      {
        "imports": {
          "@jsenv/pwa": "./node_modules/@jsenv/pwa/src/main.js"
        }
      }
    </script>
  </head>

  <body>
    <button id="add-to-home-screen" disabled>Add to home screen</button>
    <script type="module">
      import { addToHomescreen } from "@jsenv/pwa";

      const button = document.querySelector("button#add-to-home-screen");

      button.disabled = !addToHomescreen.isAvailable();
      addToHomescreen.listenAvailabilityChange(() => {
        document.querySelector("button#add-to-home-screen").disabled =
          !addToHomescreen.isAvailable();
      });
      button.onclick = () => {
        addToHomescreen.prompt();
      };
    </script>
    <!--
    "beforeinstallprompt" event might be dispatched very quickly by the navigator, before
    <script type="module"> above got a chance to catch it. For this reason, we listen
    early for this event and store it into window.beforeinstallpromptEvent.
    When "@jsenv/pwa" is imported it will check window.beforeinstallpromptEvent existence.
    -->
    <script>
      window.addEventListener(
        "beforeinstallprompt",
        (beforeinstallpromptEvent) => {
          beforeinstallpromptEvent.preventDefault();
          window.beforeinstallpromptEvent = beforeinstallpromptEvent;
        },
      );
    </script>
  </body>
</html>

addToHomescreen.isAvailable

addToHomescreen.isAvailable is a function returning a boolean indicating if addToHomescreen is available. This function must be used to know if you can call addToHomescreen.prompt.

Add to home screen is available if navigator fired a beforeinstallprompt event

addToHomescreen.listenAvailabilityChange

addToHomescreen.listenAvailabilityChange is a function that will call a callback when add to home screen becomes available or unavailable.

addToHomescreen.prompt

addToHomescreen.prompt is an async function that will ask navigator to trigger a prompt to ask user if he wants to add your website to their homescreen. It resolves to a boolean indicating if users accepted or declined the prompt.

It can be called many times but always inside a user interaction event handler, such as a click event.

displayModeStandalone

displayModeStandalone is an object that can be used to know if display mode is standalone or be notified when this changes. The standalone display mode is true when your web page is runned as an application (navigator ui is mostly/fully hidden).

import { displayModeStandalone } from "@jsenv/pwa";

displayModeStandalone.get(); // true or false

displayModeStandalone.listen(() => {
  displayModeStandalone.get(); // true or false
});

Service worker

Service worker allows you to register a script in the navigator. That script is granted with the ability to communicate with browser internal cache and intercept any request made by the navigator. They are designed to make a website capable to work offline and load instantly from cache before wondering if there is any update available. Read more on MDN documentation about service worker.

The raw service worker api offered by navigators is complex to implement. Especially when it comes to updating a service worker. This section shows how to use @jsenv/pwa to register and update a service worker.

<!doctype html>
<html>
  <head>
    <title>Title</title>
    <meta charset="utf-8" />
    <link rel="icon" href="data:," />
    <script type="importmap">
      {
        "imports": {
          "@jsenv/pwa": "./node_modules/@jsenv/pwa/src/main.js"
        }
      }
    </script>
  </head>

  <body>
    <button id="update_check_button" disabled>Check update</button>
    <p id="update_available_text"></p>
    <button id="update_activate_button" disabled>Activate update</button>
    <script type="module" src="./demo.js"></script>
  </body>
</html>

demo.js

import { createServiceWorkerFacade } from "@jsenv/pwa";

const swFacade = createServiceWorkerFacade();

const registrationPromise = window.navigator.serviceWorker.register("./sw.js");
swFacade.setRegistrationPromise(registrationPromise);

const updateCheckButton = document.querySelector("#update_check_button");
updateCheckButton.disabled = false;
updateCheckButton.onclick = async () => {
  const found = await swFacade.checkForUpdates();
  if (!found) {
    alert("no update found");
  }
};
const updateAvailableText = document.querySelector("#update_available_text");
const updateActivateButton = document.querySelector("#update_activate_button");
swFacade.subscribe(() => {
  const { update } = swFacade.state;
  if (update) {
    updateAvailableText.innerHTML = "An update is available !";
    updateActivateButton.disabled = false;
    updateActivateButton.onclick = () => {
      updateActivateButton.disabled = true;
      update.activate();
    };
  } else {
    updateAvailableText.innerHTML = "";
    buttonActivateUpdate.disabled = true;
  }
});