From 0b5d9d816ab828d32021d05a808c63d17a28dbf1 Mon Sep 17 00:00:00 2001 From: rockwalrus Date: Thu, 1 Jul 2021 13:48:53 -0700 Subject: [PATCH] Fire controlling event more than once (#2817) (#2857) * Fire controlling event more than once (#2817) Dispatches the `controlling` event every time the documentation specifies that it should. * Test tweak * One more fix Co-authored-by: Jeff Posnick --- packages/workbox-window/src/Workbox.ts | 2 +- test/workbox-window/integration/test-all.js | 4 +- test/workbox-window/window/test-Workbox.mjs | 55 ++++++++++++++++++++- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/packages/workbox-window/src/Workbox.ts b/packages/workbox-window/src/Workbox.ts index 70c305ff1..e369dddd3 100644 --- a/packages/workbox-window/src/Workbox.ts +++ b/packages/workbox-window/src/Workbox.ts @@ -189,7 +189,7 @@ class Workbox extends WorkboxEventTarget { this._registration.addEventListener('updatefound', this._onUpdateFound); navigator.serviceWorker.addEventListener( - 'controllerchange', this._onControllerChange, {once: true}); + 'controllerchange', this._onControllerChange); return this._registration; } diff --git a/test/workbox-window/integration/test-all.js b/test/workbox-window/integration/test-all.js index d3ec8698b..e77a2b568 100644 --- a/test/workbox-window/integration/test-all.js +++ b/test/workbox-window/integration/test-all.js @@ -246,12 +246,14 @@ describe(`[workbox-window] Workbox`, function() { expect(installedSpyArgs.length, 'installedSpy').to.eql(2); expect(waitingSpyArgs.length, 'waitingSpy').to.eql(0); expect(activatedSpyArgs.length, 'activatedSpy').to.eql(2); - expect(controllingSpyArgs.length, 'controllingSpy').to.eql(1); + expect(controllingSpyArgs.length, 'controllingSpy').to.eql(2); expect(installedSpyArgs[0][0].isExternal).to.eql(false); expect(activatedSpyArgs[0][0].isExternal).to.eql(false); + expect(controllingSpyArgs[0][0].isExternal).to.eql(false); expect(installedSpyArgs[1][0].isExternal).to.eql(true); expect(activatedSpyArgs[1][0].isExternal).to.eql(true); + expect(controllingSpyArgs[1][0].isExternal).to.eql(true); }); }); }); diff --git a/test/workbox-window/window/test-Workbox.mjs b/test/workbox-window/window/test-Workbox.mjs index c39abd295..30f8b15b5 100644 --- a/test/workbox-window/window/test-Workbox.mjs +++ b/test/workbox-window/window/test-Workbox.mjs @@ -846,7 +846,7 @@ describe(`[workbox-window] Workbox`, function() { // the first time a page registers a SW). This case is tested in the // integration tests. - expect(controlling1Spy.callCount).to.equal(1); + expect(controlling1Spy.callCount).to.equal(2); assertMatchesWorkboxEvent(controlling1Spy.args[0][0], { isExternal: false, isUpdate: true, @@ -855,6 +855,14 @@ describe(`[workbox-window] Workbox`, function() { target: wb1, type: 'controlling', }); + assertMatchesWorkboxEvent(controlling1Spy.args[1][0], { + isExternal: true, + isUpdate: true, + originalEvent: {type: 'controllerchange'}, + sw: await wb1.getSW(), + target: wb1, + type: 'controlling', + }); // This will be an "external" event, due to wb3's SW taking control. // wb2's SW never controls, because it's stuck in waiting. @@ -874,6 +882,51 @@ describe(`[workbox-window] Workbox`, function() { type: 'controlling', }); }); + + it(`runs every time the registered SW is updated`, async function() { + const scriptURL = uniq('sw-skip-waiting.js.njk'); + const wb1 = new Workbox(scriptURL); + const controlling1Spy = sandbox.spy(); + wb1.addEventListener('controlling', controlling1Spy); + await wb1.register(); + await nextEvent(wb1, 'controlling'); + + await updateVersion('2.0.0', scriptURL); + + wb1.update(); + await nextEvent(wb1, 'controlling'); + + await updateVersion('3.0.0', scriptURL); + + wb1.update(); + await nextEvent(wb1, 'controlling'); + + expect(controlling1Spy.callCount).to.equal(3); + assertMatchesWorkboxEvent(controlling1Spy.args[0][0], { + isExternal: false, + isUpdate: true, + originalEvent: {type: 'controllerchange'}, + sw: await wb1.getSW(), + target: wb1, + type: 'controlling', + }); + assertMatchesWorkboxEvent(controlling1Spy.args[1][0], { + isExternal: true, + isUpdate: true, + originalEvent: {type: 'controllerchange'}, + sw: await wb1.getSW(), + target: wb1, + type: 'controlling', + }); + assertMatchesWorkboxEvent(controlling1Spy.args[2][0], { + isExternal: true, + isUpdate: true, + originalEvent: {type: 'controllerchange'}, + sw: await wb1.getSW(), + target: wb1, + type: 'controlling', + }); + }); }); describe(`redundant`, function() {