w3c / w3c/ServiceWorker

Provide an easier way to listen for waiting/activated/redundant Service Workers

Open
#1,247 6 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Bikeshed
Stars
3.6k
Forks
324
Avg merge
14d 22h
Merged PRs (30d)
1

Description

Breaking off from #1222.

ServiceWorker.state has five states: installing, installed (waiting), activating, activated, and redundant.

Today, client-side code can listen for installing SWs with ServiceWorkerRegistration.onupdatefound; activating can be tracked with navigator.serviceWorker.oncontrollerchange.

The other three states are annoying to listen for.

function listenForStateChanges(reg, callback) {
  if (reg.installing) reg.installing.addEventListener('statechange', callback);
  if (reg.waiting) reg.waiting.addEventListener('statechange', callback);
  reg.addEventListener('updatefound', function() {
    reg.installing.addEventListener('statechange', callback);
  });
}

That's too bad, because lots of users want to track the installed state so they can show a refresh banner. Furthermore, some users apparently want to track redundant SWs for error handling. And it turns out that refreshing the page as the page is activating is slightly too soon; you should wait for the activated event, to avoid a minor performance bug.

In #1222 we observed that awaiting these states would be way easier if statechange bubbled up to the registration, so users who wanted to track the redundant and activated states would just do this:

reg.addEventListener('statechange', callback);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ServiceWorker.state states and the ServiceWorkerRegistration event behavior described here, then review the related discussion in #1222. Compare the existing updatefound and controllerchange paths with the proposed registration-level statechange listener; done should make waiting, activated, and redundant transitions observable through the registration.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, web-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.