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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@jsenv/pwa

v6.1.15

Published

Service worker and other progressive web application helpers

Readme

@jsenv/pwa npm package

A toolkit to implement progressive web application (PWA) features in your website.

🏠 Add to home screen functionality
🔄 Service worker lifecycle management
📱 Display mode detection
🛠️ Simple APIs for complex PWA features

Table of Contents

Installation

npm install @jsenv/pwa

Add to Home Screen

Allow users to add your website to their device homescreen, running it in a standalone mode without browser UI.

Usage Example

<!doctype html>
<html>
  <head>
    <title>PWA Demo</title>
    <meta charset="utf-8" />
    <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>

    <!-- Listen early for beforeinstallprompt event -->
    <script>
      window.addEventListener(
        "beforeinstallprompt",
        (beforeinstallpromptEvent) => {
          beforeinstallpromptEvent.preventDefault();
          window.beforeinstallpromptEvent = beforeinstallpromptEvent;
        },
      );
    </script>

    <!-- Handle add to homescreen functionality -->
    <script type="module">
      import { addToHomescreen } from "@jsenv/pwa";

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

      // Initial state
      button.disabled = !addToHomescreen.isAvailable();

      // Update when availability changes
      addToHomescreen.listenAvailabilityChange(() => {
        button.disabled = !addToHomescreen.isAvailable();
      });

      // Show prompt when clicked
      button.onclick = async () => {
        const accepted = await addToHomescreen.prompt();
        console.log(accepted ? "User accepted" : "User declined");
      };
    </script>
  </body>
</html>

API Reference

addToHomescreen.isAvailable()

Returns a boolean indicating if the "Add to Home Screen" feature is available.

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

if (addToHomescreen.isAvailable()) {
  // Enable "Add to Home Screen" button
}

The feature is available when the browser has fired a beforeinstallprompt event.

addToHomescreen.listenAvailabilityChange(callback)

Registers a callback that will be called whenever the availability of "Add to Home Screen" changes.

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

addToHomescreen.listenAvailabilityChange(() => {
  const isAvailable = addToHomescreen.isAvailable();
  console.log(
    `Add to homescreen is now ${isAvailable ? "available" : "unavailable"}`,
  );
});

addToHomescreen.prompt()

Prompts the user to add the website to their home screen. Returns a promise that resolves to a boolean indicating whether the user accepted.

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

button.onclick = async () => {
  if (!addToHomescreen.isAvailable()) {
    return;
  }

  const userAccepted = await addToHomescreen.prompt();
  if (userAccepted) {
    console.log("User added the app to home screen");
  } else {
    console.log("User declined the add to home screen prompt");
  }
};

Important: This must be called inside a user interaction event handler (like click) to work properly.

displayModeStandalone

An object to detect if the website is running in standalone mode (added to home screen).

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

// Check current mode
const isStandalone = displayModeStandalone.get();
console.log(`Running in ${isStandalone ? "standalone" : "browser"} mode`);

// Listen for mode changes
displayModeStandalone.listen(() => {
  if (displayModeStandalone.get()) {
    console.log("App is now running in standalone mode");
  } else {
    console.log("App is now running in browser mode");
  }
});

Service Worker

Service workers enable offline functionality and background updates for your web application.

Usage Example

<!doctype html>
<html>
  <head>
    <title>Service Worker Demo</title>
    <meta charset="utf-8" />
    <script type="importmap">
      {
        "imports": {
          "@jsenv/pwa": "./node_modules/@jsenv/pwa/src/main.js"
        }
      }
    </script>
  </head>
  <body>
    <button id="update-check-button" disabled>Check for updates</button>
    <p id="update-status"></p>
    <button id="update-activate-button" disabled>Activate update</button>

    <script type="module">
      import { createServiceWorkerFacade } from "@jsenv/pwa";

      // Create service worker facade
      const swFacade = createServiceWorkerFacade();

      // Register service worker
      const registration = navigator.serviceWorker.register("./sw.js");
      swFacade.setRegistrationPromise(registration);

      // UI elements
      const updateCheckButton = document.getElementById("update-check-button");
      const updateStatus = document.getElementById("update-status");
      const updateActivateButton = document.getElementById(
        "update-activate-button",
      );

      // Enable update checking
      updateCheckButton.disabled = false;
      updateCheckButton.onclick = async () => {
        updateStatus.textContent = "Checking for updates...";
        const found = await swFacade.checkForUpdates();
        if (!found) {
          updateStatus.textContent = "No updates found";
        }
      };

      // Subscribe to state changes
      swFacade.subscribe(() => {
        const { update } = swFacade.state;
        if (update) {
          updateStatus.textContent = "Update available!";
          updateActivateButton.disabled = false;
          updateActivateButton.onclick = () => {
            updateActivateButton.disabled = true;
            update.activate();
          };
        } else {
          updateStatus.textContent = "";
          updateActivateButton.disabled = true;
        }
      });
    </script>
  </body>
</html>

API Reference

createServiceWorkerFacade()