Skip to content

Commit

Permalink
feat: update workbox version
Browse files Browse the repository at this point in the history
  • Loading branch information
fvs1981 committed May 4, 2023
1 parent de1974b commit 2281419
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions addon/instance-initializers/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export default {
initialize(appInstance) {
const { autoRegister } = getConfig(appInstance);

/* istanbul ignore else */
if (autoRegister) {
initialize(appInstance);
}
Expand Down
7 changes: 7 additions & 0 deletions addon/services/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default class ServiceWorker extends EventedService {
const sw = window.navigator.serviceWorker;
let isSupported = false;

/* istanbul ignore else */
if (sw) {
isSupported = ['getRegistrations', 'register'].every(
(func) => func in sw
Expand All @@ -43,6 +44,7 @@ export default class ServiceWorker extends EventedService {
}

_log(message) {
/* istanbul ignore else */
if (this.debug) {
debug(`ember-cli-workbox: ${message}`);
}
Expand Down Expand Up @@ -90,10 +92,12 @@ export default class ServiceWorker extends EventedService {
this._log(`Registration succeeded. Scope is ${registration.scope}`);
this.trigger('registrationComplete');

/* istanbul ignore else */
if (!registration) {
return;
}

/* istanbul ignore else */
if (registration.waiting) {
// SW is waiting to activate. Can occur if multiple clients open and
// one of the clients is refreshed.
Expand All @@ -105,6 +109,7 @@ export default class ServiceWorker extends EventedService {
registration.addEventListener('updatefound', () => {
const installingWorker = registration.installing;

/* istanbul ignore else */
if (!installingWorker) {
return;
}
Expand All @@ -120,8 +125,10 @@ export default class ServiceWorker extends EventedService {
}

_checkSWInstalled(installingWorker, registration) {
console.log('state: ' + installingWorker.state);
switch (installingWorker.state) {
case 'installed':
console.log('case installed ');
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = {
env = env || process.env.EMBER_ENV;

// Do nothing if no ENV. For example, when running an ember generator.
/* istanbul ignore else */
if (!env) {
return;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/broccoli-workbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class BroccoliWorkbox extends Plugin {
}

removeServiceWorker(filePath) {
/* istanbul ignore else */
if (fs.existsSync(filePath)) {
debug(yellow('Addon disabled. Service worker exist, remove it'));
fs.unlinkSync(filePath);
Expand All @@ -56,6 +57,7 @@ class BroccoliWorkbox extends Plugin {
)
);

/* istanbul ignore else */
if (warnings) {
debug(yellow(warnings));
}
Expand All @@ -72,6 +74,7 @@ class BroccoliWorkbox extends Plugin {

workboxOptions.swDest = path.join(directory, workboxOptions.swDest);

/* istanbul ignore else */
if (!this.options.enabled) {
debug(yellow('Addon disabled. Disable and remove service worker...'));

Expand All @@ -92,6 +95,7 @@ class BroccoliWorkbox extends Plugin {
);

// Remove workbox libraries directory to prevent exception on recopying it.
/* istanbul ignore else */
if (
!workboxOptions.importWorkboxFromCDN &&
fs.existsSync(workboxDirectory)
Expand All @@ -111,7 +115,7 @@ class BroccoliWorkbox extends Plugin {
);

workboxOptions.importScripts = filesToIncludeInSW;

/* istanbul ignore else */
if (this.options.importScriptsTransform) {
workboxOptions.importScripts = this.options.importScriptsTransform(
workboxOptions.importScripts
Expand Down
36 changes: 36 additions & 0 deletions tests/acceptance/simple-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import sinon from 'sinon';

module('Acceptance | Simple Acceptance Test', (hooks) => {
setupApplicationTest(hooks);
Expand Down Expand Up @@ -48,6 +49,8 @@ module('Acceptance | Simple Acceptance Test', (hooks) => {
const registrations =
await window.navigator.serviceWorker.getRegistrations();

console.log('ave4r si ' + JSON.stringify(registrations));

assert.ok(registrations.length);
});

Expand All @@ -74,6 +77,39 @@ module('Acceptance | Simple Acceptance Test', (hooks) => {
assert.notOk(registrations.length);
});

test('it unregisters sw but fails', async function (assert) {
await this.swService.register('sw.js');

const registrations = await this.swService.sw.getRegistrations();
const stubs = registrations.map((reg) => {
return sinon.stub(reg, 'unregister').callsFake(() => {
return Promise.resolve(false);
});
});

const stubLog = sinon.stub(this.swService, '_log');

assert.deepEqual(
this.events,
['registrationComplete'],
'Event triggered: registrationComplete'
);

await this.swService.unregisterAll();

assert.ok(stubLog.called);
assert.ok(registrations.length);

assert.deepEqual(
this.events,
['registrationComplete', 'unregistrationComplete'],
'Service worker does not exists'
);

stubs.forEach((stubReg) => stubReg.restore());
stubLog.restore();
});

test.skip('it triggers "update" event on sw response', async function (assert) {
const _reload = window.location.reload;
let called = 0;
Expand Down

0 comments on commit 2281419

Please sign in to comment.