Menu

#3377 The `WorkboxLifecycleEvent.isUpdate` is false on first immediate service worker update

open
nobody
None
2024-11-22
2024-11-22
Anonymous
No

Originally created by: piotr-cz

Library Affected:
workbox-window

Browser & Platform:
At least Chrome 131.0.6778.86, possibly all browsers

Issue or Feature Request Description:

The WorkboxLifecycleEvent.isUpdate will remain false in following scenario:

  1. New service worker installation
  2. Alter source code to trigger update in next step
  3. Manually trigger Service worker update (without navigation/ page reload), for ie. by one of;
  4. DevTools > Application > Service workers > Update
  5. (await navigator.serviceWorker.getRegistration()).update()

This matches the behavior described in source:

https://github.com/GoogleChrome/workbox/blob/c77dceb54d4af1749db95316710d6430e82b0c48/packages/workbox-window/src/Workbox.ts#L698-L699

However it's name leads to false assumption that it's value is true on any update (even that there was no controlling service worker at the time of registration).

Some examples:

When a new service worker starts installing but an existing version is currently controlling the page, the isUpdate property of all the following events will be true.

ts wb.addEventListener('installed', (event) => { if (event.isUpdate) { // Show "Update App" banner } });

https://github.com/GoogleChrome/workbox/blob/c77dceb54d4af1749db95316710d6430e82b0c48/demos/src/workbox-window/index.html#L53-L57

Suggested solution

I think the fix is to set this._isUpdate = true after first installation:

diff --git a/packages/workbox-window/src/Workbox.ts
    private readonly _onControllerChange = (originalEvent: Event) => {
      const sw = this._sw;
      const isExternal = sw !== navigator.serviceWorker.controller;


+     this._isUpdate = true
+     this._isUpdate = navigator.serviceWorker.controller !== null // Alternative implementation

and adjust jsdoc comments for this property.

I'm just not entirely sure if this is the right place?

Related PR: https://github.com/GoogleChrome/workbox/pull/1905

Discussion


Log in to post a comment.