Skip to content

Commit

Permalink
Merge pull request #1941 from GoogleChrome/workbox-window-update
Browse files Browse the repository at this point in the history
Resolve getSW correctly after an update is found
  • Loading branch information
philipwalton authored Mar 6, 2019
2 parents eab861f + 46f1c2b commit 5e2c4a1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/workbox-window/Workbox.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ class Workbox extends EventTargetShim {
* @return {Promise<ServiceWorker>}
*/
async getSW() {
return this._swDeferred.promise;
// If `this._sw` is set, resolve with that as we want `getSW()` to
// return the correct (new) service worker if an update is found.
return this._sw || this._swDeferred.promise;
}

/**
Expand Down
29 changes: 29 additions & 0 deletions test/workbox-window/unit/test-Workbox.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,35 @@ describe(`[workbox-window] Workbox`, function() {
const sw = await wb.getSW();
expect(sw.state).to.equal('installing');
});

it(`resolves to the new SW after an update is found`, async function() {
const scriptURL = navigator.serviceWorker.controller.scriptURL;

// Update the SW after it's controlling so both an original compatible
// controller is found **and** and update is found. We need to assert
// that the `getSW()` method resolves to the correct SW in both cases.
await updateVersion('2.0.0', scriptURL);

const wb = new Workbox(scriptURL);

// Registering using the same script URL that's already active won't
// trigger an update.
const regPromise = wb.register();

const controllingSW = await wb.getSW();
expect(controllingSW).to.equal(navigator.serviceWorker.controller);

const reg = await regPromise;

// Force an update check.
reg.update();

await nextEvent(wb, 'controlling');

const installedSW = await wb.getSW();
expect(installedSW).to.equal(reg.active);
expect(installedSW).to.not.equal(controllingSW);
});
});

describe(`messageSW`, function() {
Expand Down

0 comments on commit 5e2c4a1

Please sign in to comment.