Socket
Socket
Sign inDemoInstall

workbox-window

Package Overview
Dependencies
2
Maintainers
6
Versions
59
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    workbox-window

Simplifies communications with Workbox packages running in the service worker


Version published
Weekly downloads
4.2M
decreased by-0.16%
Maintainers
6
Created
Weekly downloads
 

Package description

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

Readme

Source

This module's documentation can be found at https://developers.google.com/web/tools/workbox/modules/workbox-window

Keywords

FAQs

Last updated on 23 Apr 2024

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc