Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

messageSkipWaiting() method for workbox-window #2489

Merged
merged 3 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// {{ version }}

addEventListener('message', async (event) => {
addEventListener('message', (event) => {
if (event.data.type === 'SKIP_WAITING') {
skipWaiting();
}
Expand Down
17 changes: 17 additions & 0 deletions packages/workbox-window/src/Workbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const WAITING_TIMEOUT_DURATION = 200;
// that the registration didn't trigger an update.
const REGISTRATION_TIMEOUT_DURATION = 60000;

// The de facto standard message that a service worker should be listening for
// to trigger a call to skipWaiting().
const SKIP_WAITING_MESSAGE = {type: 'SKIP_WAITING'};

/**
* A class to aid in handling service worker registration, updates, and
* reacting to service worker lifecycle events.
Expand Down Expand Up @@ -277,6 +281,19 @@ class Workbox extends WorkboxEventTarget {
return messageSW(sw, data);
}

/**
* Sends a `{type: 'SKIP_WAITING'}` message to the service worker that's
* currently in the `waiting` state associated with the current registration.
*
* If there is no current registration or no service worker is `waiting`,
* calling this will have no effect.
*/
messageSkipWaiting() {
if (this._registration && this._registration.waiting) {
messageSW(this._registration.waiting, SKIP_WAITING_MESSAGE);
}
}

/**
* Checks for a service worker already controlling the page and returns
* it if its script URL matches.
Expand Down
45 changes: 42 additions & 3 deletions test/workbox-window/window/test-Workbox.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import {Workbox} from '/__WORKBOX/buildFile/workbox-window';


const isDev = () => {
return self.process && self.process.env &&
self.process.env.NODE_ENV !== 'production';
Expand Down Expand Up @@ -45,7 +44,6 @@ const uniq = (() => {
};
})();


const stubAlreadyControllingSW = async (scriptURL) => {
await navigator.serviceWorker.register(scriptURL);
await waitUntil(() => {
Expand Down Expand Up @@ -518,6 +516,47 @@ describe(`[workbox-window] Workbox`, function() {
});
});

describe(`messageSkipWaiting`, function() {
it(`posts the expected message to the waiting service worker`, async function() {
const scriptURL = uniq('sw-skip-waiting-on-message.js.njk');
const wb = new Workbox(scriptURL);

const waitingSWPromise = new Promise((resolve) => {
wb.addEventListener('waiting', (event) => resolve(event.sw));
});

await wb.register();
const waitingSW = await waitingSWPromise;

const postMessageSpy = sandbox.spy(waitingSW, 'postMessage');

const controllingSWPromise = new Promise((resolve) => {
wb.addEventListener('controlling', (event) => resolve(event.sw));
});

wb.messageSkipWaiting();

// Confirm that the right postMessage() is sent.
expect(postMessageSpy.callCount).to.eql(1);
expect(postMessageSpy.firstCall.args[0]).to.eql({type: 'SKIP_WAITING'});
expect(postMessageSpy.firstCall.args[1][0]).to.be.instanceOf(MessagePort);

const controllingSW = await controllingSWPromise;

// Confirm that the service worker associated with this test takes control.
expect(controllingSW.scriptURL).to.eql(scriptURL);
});

it(`does nothing if there's no waiting service worker`, async function() {
const wb = new Workbox(uniq('sw-skip-waiting.js.njk'));
await wb.register();

// This should be a no-op.
// Just ensure that there's no exceptions thrown, etc.
wb.messageSkipWaiting();
});
});

describe(`events`, function() {
describe(`message`, function() {
it(`fires when a postMessage is received from the SW`, async function() {
Expand Down Expand Up @@ -939,7 +978,7 @@ describe(`[workbox-window] Workbox`, function() {
toFake: ['performance'],
});

const scriptURL = uniq('sw-skip-waiting-on-mesage.js.njk');
const scriptURL = uniq('sw-skip-waiting-on-message.js.njk');
const wb = new Workbox(scriptURL);
const reg = await wb.register();

Expand Down