Skip to content

Commit

Permalink
Fix service worker gatherer by waiting for active state (#1864)
Browse files Browse the repository at this point in the history
* Wait for active serviceworker

* Fix test cases

* Review fixes

* Review fix
  • Loading branch information
wardpeet authored and ebidel committed Apr 26, 2017
1 parent 4f598c5 commit 4c34e28
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 10 deletions.
22 changes: 18 additions & 4 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,24 @@ class Driver {

getServiceWorkerVersions() {
return new Promise((resolve, reject) => {
this.once('ServiceWorker.workerVersionUpdated', data => {
this.sendCommand('ServiceWorker.disable')
.then(_ => resolve(data), reject);
});
const versionUpdatedListener = data => {
// find a service worker with runningStatus that looks like active
// on slow connections the serviceworker might still be installing
const activateCandidates = data.versions.filter(sw => {
return sw.status !== 'redundant';
});
const hasActiveServiceWorker = activateCandidates.find(sw => {
return sw.status === 'activated';
});

if (!activateCandidates.length || hasActiveServiceWorker) {
this.off('ServiceWorker.workerVersionUpdated', versionUpdatedListener);
this.sendCommand('ServiceWorker.disable')
.then(_ => resolve(data), reject);
}
};

this.on('ServiceWorker.workerVersionUpdated', versionUpdatedListener);

this.sendCommand('ServiceWorker.enable').catch(reject);
});
Expand Down
58 changes: 52 additions & 6 deletions lighthouse-core/test/gather/driver-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ function createSWRegistration(id, url, isDeleted) {
};
}

function createActiveWorker(id, url, controlledClients) {
function createActiveWorker(id, url, controlledClients, status = 'activated') {
return {
registrationId: id,
scriptURL: url,
controlledClients,
status,
};
}

Expand Down Expand Up @@ -199,9 +200,12 @@ describe('Multiple tab check', () => {
'ServiceWorker.workerRegistrationUpdated': {
registrations: []
},
});

driverStub.on = createOnceStub({
'ServiceWorker.workerVersionUpdated': {
versions: []
}
},
});

return driverStub.assertNoSameOriginServiceWorkerClients(pageUrl);
Expand All @@ -223,9 +227,12 @@ describe('Multiple tab check', () => {
'ServiceWorker.workerRegistrationUpdated': {
registrations
},
});

driverStub.on = createOnceStub({
'ServiceWorker.workerVersionUpdated': {
versions
}
},
});

return driverStub.assertNoSameOriginServiceWorkerClients(pageUrl);
Expand All @@ -244,10 +251,13 @@ describe('Multiple tab check', () => {
driverStub.once = createOnceStub({
'ServiceWorker.workerRegistrationUpdated': {
registrations
},
}
});

driverStub.on = createOnceStub({
'ServiceWorker.workerVersionUpdated': {
versions
}
},
});

return driverStub.assertNoSameOriginServiceWorkerClients(pageUrl)
Expand All @@ -267,11 +277,47 @@ describe('Multiple tab check', () => {
'ServiceWorker.workerRegistrationUpdated': {
registrations
},
});

driverStub.on = createOnceStub({
'ServiceWorker.workerVersionUpdated': {
versions
}
},
});

return driverStub.assertNoSameOriginServiceWorkerClients(pageUrl);
});

it('will wait for serviceworker to be activated', () => {
const pageUrl = 'https://example.com/';
const swUrl = `${pageUrl}sw.js`;
const registrations = [createSWRegistration(1, pageUrl)];
const versions = [createActiveWorker(1, swUrl, [], 'installing')];

driverStub.once = createOnceStub({
'ServiceWorker.workerRegistrationUpdated': {
registrations
},
});

driverStub.on = (eventName, cb) => {
if (eventName === 'ServiceWorker.workerVersionUpdated') {
cb({versions});

setTimeout(() => {
cb({
versions: [
createActiveWorker(1, swUrl, [], 'activated'),
]
});
}, 1000);

return;
}

throw Error(`Stub not implemented: ${eventName}`);
};

return driverStub.assertNoSameOriginServiceWorkerClients(pageUrl);
});
});

0 comments on commit 4c34e28

Please sign in to comment.