From c22b87174aa2d07eadaa18d11b1d286943f7ffe8 Mon Sep 17 00:00:00 2001 From: Nathan Summers Date: Wed, 12 May 2021 13:21:25 -0700 Subject: [PATCH 1/3] Fire controlling event more than once (#2817) Dispatches the `controlling` event every time the documentation specifies that it should. --- packages/workbox-window/src/Workbox.ts | 2 +- test/workbox-window/window/test-Workbox.mjs | 55 ++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/packages/workbox-window/src/Workbox.ts b/packages/workbox-window/src/Workbox.ts index b018b4c2c..94013f576 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/window/test-Workbox.mjs b/test/workbox-window/window/test-Workbox.mjs index 5ad073c90..cc84522c7 100644 --- a/test/workbox-window/window/test-Workbox.mjs +++ b/test/workbox-window/window/test-Workbox.mjs @@ -844,7 +844,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, @@ -853,6 +853,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. @@ -872,6 +880,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() { From 82f8e96c40daa3d17073266976ccd4174496f432 Mon Sep 17 00:00:00 2001 From: Jeff Posnick Date: Tue, 29 Jun 2021 13:31:54 -0400 Subject: [PATCH 2/3] Test tweak --- test/workbox-window/integration/test-all.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/workbox-window/integration/test-all.js b/test/workbox-window/integration/test-all.js index d3ec8698b..361526119 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[1][0].isExternal).to.eql(true); expect(installedSpyArgs[1][0].isExternal).to.eql(true); expect(activatedSpyArgs[1][0].isExternal).to.eql(true); + expect(controllingSpyArgs[1][0].isExternal).to.eql(true); }); }); }); From 5e582734edaffbf132a48f692701543f24da81fc Mon Sep 17 00:00:00 2001 From: Jeff Posnick Date: Tue, 29 Jun 2021 15:41:16 -0400 Subject: [PATCH 3/3] One more fix --- test/workbox-window/integration/test-all.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/workbox-window/integration/test-all.js b/test/workbox-window/integration/test-all.js index 361526119..e77a2b568 100644 --- a/test/workbox-window/integration/test-all.js +++ b/test/workbox-window/integration/test-all.js @@ -250,7 +250,7 @@ describe(`[workbox-window] Workbox`, function() { expect(installedSpyArgs[0][0].isExternal).to.eql(false); expect(activatedSpyArgs[0][0].isExternal).to.eql(false); - expect(controllingSpyArgs[1][0].isExternal).to.eql(true); + 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);