Skip to content

Commit db69edd

Browse files
committed
update offline gatherer to use network recording changes
1 parent 34509b1 commit db69edd

File tree

5 files changed

+29
-31
lines changed

5 files changed

+29
-31
lines changed

lighthouse-cli/test/fixtures/smoketest-offline-config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
]
77
},{
88
"loadPage": true,
9+
"network": true,
910
"gatherers": [
1011
"service-worker",
1112
"offline"

lighthouse-core/config/default.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
]
1818
},
1919
{
20+
"passName": "offlinePass",
2021
"loadPage": true,
2122
"network": true,
2223
"gatherers": [

lighthouse-core/gather/gatherers/offline.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
1817
'use strict';
1918

2019
const Gatherer = require('./gatherer');
@@ -43,26 +42,17 @@ class Offline extends Gatherer {
4342
}
4443

4544
beforePass(options) {
46-
const driver = options.driver;
47-
48-
return driver.gotoURL(options.url, {waitForLoad: true})
49-
.then(_ => Offline.goOffline(driver))
50-
// Navigate away, then back, to allow a service worker that doesn't call
51-
// clients.claim() to take control of the page load.
52-
.then(_ => driver.gotoURL('about:blank'))
53-
.then(_ => driver.gotoURL(options.url, {waitForLoad: true}))
54-
.then(_ => Offline.goOnline(driver));
45+
return Offline.goOffline(options.driver);
5546
}
5647

5748
afterPass(options, tracingData) {
5849
const navigationRecord = tracingData.networkRecords.filter(record => {
59-
// If options.url is just an origin without a path, the Chrome will
60-
// implicitly add in a path of '/'.
61-
return (record._url === options.url || record._url === options.url + '/') &&
62-
record._fetchedViaServiceWorker;
50+
return record._url === options.url && record._fetchedViaServiceWorker;
6351
}).pop(); // Take the last record that matches.
6452

6553
this.artifact = navigationRecord ? navigationRecord.statusCode : -1;
54+
55+
return Offline.goOnline(options.driver);
6656
}
6757
}
6858

lighthouse-core/lib/network-recorder.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,8 @@ class NetworkRecorder extends EventEmitter {
8787
}
8888

8989
onResourceChangedPriority(data) {
90-
// TODO: this.networkManager._dispatcher.resourceChangedPriority is set to
91-
// undefined when onResourceChangedPriority is triggered in
92-
// gatherers/offline.js. The underlying cause may need to be investigated.
93-
// In the meantime, explicitly check that it's a function.
94-
if (typeof this.networkManager._dispatcher.resourceChangedPriority === 'function') {
95-
this.networkManager._dispatcher.resourceChangedPriority(data.requestId,
90+
this.networkManager._dispatcher.resourceChangedPriority(data.requestId,
9691
data.newPriority, data.timestamp);
97-
}
9892
}
9993

10094
static recordsFromLogs(logs) {

lighthouse-core/test/gather/gatherers/offline-test.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,33 @@
2020
const OfflineGather = require('../../../gather/gatherers/offline');
2121
const assert = require('assert');
2222
const tracingData = require('../../fixtures/traces/network-records.json');
23-
let offlineGather;
2423

25-
describe('Offline gatherer', () => {
26-
// Reset the Gatherer before each test.
27-
beforeEach(() => {
28-
offlineGather = new OfflineGather();
29-
});
24+
const mockDriver = {
25+
sendCommand() {
26+
return Promise.resolve();
27+
}
28+
};
3029

30+
describe('Offline gatherer', () => {
3131
it('returns an artifact set to -1 when offline loading fails', () => {
32-
offlineGather.afterPass({url: 'https://do-not-match.com'}, tracingData);
33-
assert.deepEqual(offlineGather.artifact, -1);
32+
const offlineGather = new OfflineGather();
33+
const options = {
34+
url: 'https://do-not-match.com/',
35+
driver: mockDriver
36+
};
37+
return offlineGather.afterPass(options, tracingData).then(_ => {
38+
assert.strictEqual(offlineGather.artifact, -1);
39+
});
3440
});
3541

3642
it('returns an artifact set to 200 when offline loading succeeds', () => {
37-
offlineGather.afterPass({url: 'https://ifixit-pwa.appspot.com'}, tracingData);
38-
assert.deepEqual(offlineGather.artifact, 200);
43+
const offlineGather = new OfflineGather();
44+
const options = {
45+
url: 'https://ifixit-pwa.appspot.com/',
46+
driver: mockDriver
47+
};
48+
return offlineGather.afterPass(options, tracingData).then(_ => {
49+
assert.strictEqual(offlineGather.artifact, 200);
50+
});
3951
});
4052
});

0 commit comments

Comments
 (0)