Skip to content

Commit

Permalink
Removes hash fragments from url so serviceworker does a correct offli…
Browse files Browse the repository at this point in the history
…ne check.

We store the url inside options.url without a fragment so we should do this check without fragment as well.
Fixes PR #1296
  • Loading branch information
wardpeet committed Dec 26, 2016
1 parent aa1059b commit 11229bc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
8 changes: 7 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 cleanHash(new URL(record._url)) === cleanHash(new URL(options.url)) &&
record._fetchedViaServiceWorker;
}).pop(); // Take the last record that matches.

return options.driver.goOnline(options).then(_ => {
Expand All @@ -34,4 +36,8 @@ class Offline extends Gatherer {
}
}

function cleanHash(url) {
return url.href.removeURLFragment();
}

module.exports = Offline;
23 changes: 20 additions & 3 deletions lighthouse-core/test/gather/gatherers/offline-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,25 @@ 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
};
const optionsWithQueryString = {
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);
}),
offlineGather.afterPass(optionsWithQueryString, tracingData).then(artifact => {
assert.strictEqual(artifact, -1);
}),
]);
});
});

0 comments on commit 11229bc

Please sign in to comment.