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

sw-update-manager

v1.0.0

Published

A front-end package that allows you to detect updates to your service worker and prompt the user to update with a notification.

Downloads

8

Readme

SW Update Manager

Copyright 2018 Caleb Evans
Released under the MIT license

SW Update Manager is a front-end package that allows you to detect updates to your service worker and prompt the user to update with a notification.

This project has now hit v1.0, so the API is now stable.

Getting Started

1. Load the script into your application

First, you need to load the SW Update Manager script into the front-end portion of your application. You can do this via <script> tag or through your build process.

<script src="node_modules/sw-update-manager/sw-update-manager.js"></script>
var SWUpdateManager = require('sw-update-manager');
import SWUpdateManager from 'sw-update-manager';

2. Add front-end JS

In your front-end JavaScript, you will need to initialize a new SWUpdateManager instance and ensure it is accessible by the code that spawns the update notification.

if (navigator.serviceWorker) {
  let serviceWorker = navigator.serviceWorker.register('service-worker.js');
  this.updateManager = new SWUpdateManager(serviceWorker);
  // Use the optional updateAvailable hook to re-render the UI (or run any other
  // necessary code) when an update is available
  this.updateManager.on('updateAvailable', () => this.render());
  // You must explicitly tell SW Update Manager when to check for updates to the
  // service worker
  this.updateManager.checkForUpdates();
}

You can use the isUpdateAvailable property on your SWUpdateManager object to decide when to display the update notification.

<div className={`update-notification ${this.updateManager.isUpdateAvailable ? 'visible': 'hidden'}`}></div>

Whenever you decide to trigger the service worker update, call the update() method on your SWUpdateManager object. You can call update() however you want, but for a simple user experience, bind a click event to the update notification you show to the user (a long as you instruct the user to click the notification to update).

<div className='update-notification' onclick={() => this.updateManager.update()}></div>

All of these code examples are using React/JSX because the syntax is familiar to many developers. However, you could effortlessly use any of these features with another library or in vanilla JavaScript.

3. Listen for update requests to your service worker

The final step in configuring SW Update Manager is to listen for events from the service worker itself.

// When an update to the service worker is detected, the front end will request
// that the service worker be updated immediately; listen for that request here
self.addEventListener('message', (event) => {
  if (!event.data) {
    return;
  }
  if (event.data.updateManagerEvent === 'update') {
    self.skipWaiting();
  }
});

Other API features

Controlling update behavior

If you'd like to stop SW Update Manager from reloading the page automatically when calling update(), set reloadOnUpdate to false. You can do this on initialization on the update manager, or afterwards.

this.updateManager = new SWUpdateManager(serviceWorker, {
  reloadOnUpdate: false
});
this.updateManager = new SWUpdateManager(serviceWorker);
this.updateManager.reloadOnUpdate = false;

You can also run your own code before the actual update takes place by listening for the update event.

// The page will still reload automatically after this callback fires, unless
// you set reloadOnUpdate to false as described above
this.updateManager.on('update', () => {
  localStorage.setItem('lastUpdateTime', Date.now());
});

The update event fires only when the service worker is actually able to be updated. Calling the update() method a second time will not fire the event callbacks again, nor will the callbacks fire at all if the service worker is missing the aforementioned listener code.