Skip to content

Commit

Permalink
core(start-url): stay offline for entirety of offlinePass (#9451)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce authored Aug 15, 2019
1 parent 69ce8ef commit 6861412
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 129 deletions.
8 changes: 5 additions & 3 deletions lighthouse-core/gather/gatherers/offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class Offline extends Gatherer {
* @param {LH.Gatherer.PassContext} passContext
*/
beforePass(passContext) {
// This call sets up the offline state for the page navigation of the `offlinePass` in gather-runner.
// gather-runner will automatically go back online before the `afterPass` phase, so no additional
// cleanup is necessary.
return passContext.driver.goOffline();
}

Expand All @@ -21,14 +24,13 @@ class Offline extends Gatherer {
* @param {LH.Gatherer.LoadData} loadData
* @return {Promise<LH.Artifacts['Offline']>}
*/
afterPass(passContext, loadData) {
async afterPass(passContext, loadData) {
const navigationRecord = loadData.networkRecords.filter(record => {
return URL.equalWithExcludedFragments(record.url, passContext.url) &&
record.fetchedViaServiceWorker;
}).pop(); // Take the last record that matches.

return passContext.driver.goOnline(passContext)
.then(_ => navigationRecord ? navigationRecord.statusCode : -1);
return navigationRecord ? navigationRecord.statusCode : -1;
}
}

Expand Down
28 changes: 22 additions & 6 deletions lighthouse-core/gather/gatherers/start-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,36 @@ const Gatherer = require('./gatherer.js');

class StartUrl extends Gatherer {
/**
* Grab the manifest, extract it's start_url, attempt to `fetch()` it while offline
* Go offline, assess the start url, go back online.
* @param {LH.Gatherer.PassContext} passContext
* @return {Promise<LH.Artifacts['StartUrl']>}
*/
async afterPass(passContext) {
// `afterPass` is always online, so manually go offline to check start_url.
await passContext.driver.goOffline();
const result = await this._determineStartUrlAvailability(passContext);
await passContext.driver.goOnline(passContext);

return result;
}

/**
* Grab the manifest, extract its start_url, attempt to `fetch()` it while offline
* @param {LH.Gatherer.PassContext} passContext
* @return {Promise<LH.Artifacts['StartUrl']>}
*/
async _determineStartUrlAvailability(passContext) {
const manifest = passContext.baseArtifacts.WebAppManifest;
const startUrlInfo = this._readManifestStartUrl(manifest);
if (startUrlInfo.isReadFailure) {
return {statusCode: -1, explanation: startUrlInfo.reason};
}

return this._attemptStartURLFetch(passContext.driver, startUrlInfo.startUrl).catch(() => {
return {statusCode: -1, explanation: 'Unable to fetch start URL via service worker.'};
});
try {
return await this._attemptStartURLFetch(passContext.driver, startUrlInfo.startUrl);
} catch (err) {
return {statusCode: -1, explanation: 'Error while fetching start_url via service worker.'};
}
}

/**
Expand Down Expand Up @@ -59,7 +75,7 @@ class StartUrl extends Gatherer {
// Wait up to 3s to get a matched network request from the fetch() to work
const timeoutPromise = new Promise(resolve =>
setTimeout(
() => resolve({statusCode: -1, explanation: 'Timed out waiting for fetched start_url.'}),
() => resolve({statusCode: -1, explanation: 'Timed out waiting for start_url to respond.'}),
3000
)
);
Expand All @@ -77,7 +93,7 @@ class StartUrl extends Gatherer {
if (!response.fromServiceWorker) {
return resolve({
statusCode: -1,
explanation: 'Unable to fetch start URL via service worker.',
explanation: 'The start_url did respond, but not via a service worker.',
});
}
// Successful SW-served fetch of the start_URL
Expand Down
Loading

0 comments on commit 6861412

Please sign in to comment.