Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes hash fragments from url in offline audit #1319

Merged
merged 3 commits into from
Jan 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lighthouse-core/gather/gatherers/offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'use strict';

const Gatherer = require('./gatherer');
const URL = require('../../lib/url-shim');

class Offline extends Gatherer {
beforePass(options) {
Expand All @@ -25,7 +26,8 @@ class Offline extends Gatherer {

afterPass(options, tracingData) {
const navigationRecord = tracingData.networkRecords.filter(record => {
return record._url === options.url && record._fetchedViaServiceWorker;
return URL.equalWithExcludedFragments(record._url, options.url) &&
record._fetchedViaServiceWorker;
}).pop(); // Take the last record that matches.

return options.driver.goOnline(options).then(_ => {
Expand Down
17 changes: 16 additions & 1 deletion lighthouse-core/lib/url-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ URL.hostsMatch = function hostsMatch(urlA, urlB) {
}
};


/**
* @param {string} url
* @return {string}
Expand Down Expand Up @@ -95,4 +94,20 @@ URL.getDisplayName = function getDisplayName(url) {
return name;
};

/**
* Determine if url1 equals url2, ignoring URL fragments.
* @param {string} url1
* @param {string} url2
* @return {boolean}
*/
URL.equalWithExcludedFragments = function(url1, url2) {
url1 = new URL(url1);
url1.hash = '';

url2 = new URL(url2);
url2.hash = '';

return url1.href === url2.href;
};

module.exports = URL;
31 changes: 25 additions & 6 deletions lighthouse-core/test/gather/gatherers/offline-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,19 @@ describe('Offline gatherer', () => {
url: 'https://do-not-match.com/',
driver: mockDriver
};
return offlineGather.afterPass(options, tracingData).then(artifact => {
assert.strictEqual(artifact, -1);
});
const optionsWithQueryString = {
url: 'https://ifixit-pwa.appspot.com/?history',
driver: mockDriver
};

return Promise.all([
offlineGather.afterPass(options, tracingData).then(artifact => {
assert.strictEqual(artifact, -1);
}),
offlineGather.afterPass(optionsWithQueryString, tracingData).then(artifact => {
assert.strictEqual(artifact, -1);
}),
]);
});

it('returns an artifact set to 200 when offline loading succeeds', () => {
Expand All @@ -48,8 +58,17 @@ describe('Offline gatherer', () => {
url: 'https://ifixit-pwa.appspot.com/',
driver: mockDriver
};
return offlineGather.afterPass(options, tracingData).then(artifact => {
assert.strictEqual(artifact, 200);
});
const optionsWithFragment = {
url: 'https://ifixit-pwa.appspot.com/#/history',
driver: mockDriver
};
return Promise.all([
offlineGather.afterPass(options, tracingData).then(artifact => {
assert.strictEqual(artifact, 200);
}),
offlineGather.afterPass(optionsWithFragment, tracingData).then(artifact => {
assert.strictEqual(artifact, 200);
}),
]);
});
});