Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

workbox-window

Package Overview
Dependencies
Maintainers
6
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

workbox-window

Simplifies communications with Workbox packages running in the service worker

  • 7.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
6
Created

What is workbox-window?

The workbox-window npm package is a module designed to simplify the integration of Workbox into a web application. It provides an easy-to-use interface for registering and communicating with a service worker, handling service worker updates, and managing the lifecycle events of the service worker within the context of a web page.

What are workbox-window's main functionalities?

Registering a service worker

This feature allows you to register a service worker with ease. The Workbox object is instantiated with the path to the service worker file, and the register method is called to start the registration process.

import { Workbox } from 'workbox-window';

if ('serviceWorker' in navigator) {
  const wb = new Workbox('/sw.js');

  wb.register();
}

Listening to service worker lifecycle events

This feature allows you to add event listeners for various service worker lifecycle events such as 'installed', 'activated', and 'waiting'. This example shows how to listen for the 'installed' event and log messages depending on whether it's an update or a first-time installation.

import { Workbox } from 'workbox-window';

const wb = new Workbox('/sw.js');

wb.addEventListener('installed', (event) => {
  if (event.isUpdate) {
    console.log('Service worker has been updated!');
  } else {
    console.log('Service worker has been installed for the first time!');
  }
});

wb.register();

Prompting user to update the service worker

This feature allows you to prompt the user when a new service worker version is available and waiting to be activated. The 'waiting' event is used to detect this state, and 'messageSkipWaiting' is called to activate the new service worker if the user confirms.

import { Workbox } from 'workbox-window';

const wb = new Workbox('/sw.js');

wb.addEventListener('waiting', () => {
  if (confirm('A new version is available, update now?')) {
    wb.messageSkipWaiting();
  }
});

wb.register();

Other packages similar to workbox-window

Keywords

FAQs

Package last updated on 29 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc