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

core(main-resource): adjust main resource identification logic #4475

Merged
merged 4 commits into from
Feb 15, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 1 addition & 12 deletions lighthouse-core/gather/computed/main-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
'use strict';

const ComputedArtifact = require('./computed-artifact');
const HTTP_REDIRECT_CODE_LOW = 300;
const HTTP_REDIRECT_CODE_HIGH = 399;

/**
* @fileoverview This artifact identifies the main resource on the page. Current solution assumes
Expand All @@ -18,15 +16,6 @@ class MainResource extends ComputedArtifact {
return 'MainResource';
}

/**
* @param {WebInspector.NetworkRequest} record
* @return {boolean}
*/
isMainResource(request) {
return request.statusCode < HTTP_REDIRECT_CODE_LOW ||
request.statusCode > HTTP_REDIRECT_CODE_HIGH;
}

/**
* @param {!DevtoolsLog} devtoolsLog
* @param {!ComputedArtifacts} artifacts
Expand All @@ -35,7 +24,7 @@ class MainResource extends ComputedArtifact {
compute_(devtoolsLog, artifacts) {
return artifacts.requestNetworkRecords(devtoolsLog)
.then(requests => {
const mainResource = requests.find(this.isMainResource);
const mainResource = requests.find(request => request.url === artifacts.URL.finalUrl);

if (!mainResource) {
throw new Error('Unable to identify the main resource');
Expand Down
30 changes: 6 additions & 24 deletions lighthouse-core/test/gather/computed/main-resource-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ describe('MainResource computed artifact', () => {

it('returns an artifact', () => {
const record = {
statusCode: 404,
url: 'https://example.com',
};
const networkRecords = [
{url: 'http://example.com'},
record,
];
computedArtifacts.requestNetworkRecords = _ => Promise.resolve(networkRecords);
computedArtifacts.URL = {finalUrl: 'https://example.com'};

return computedArtifacts.requestMainResource({}).then(output => {
assert.equal(output, record);
Expand All @@ -33,11 +35,10 @@ describe('MainResource computed artifact', () => {

it('thows when main resource can\'t be found', () => {
const networkRecords = [
{
statusCode: 302,
},
{url: 'https://example.com'},
];
computedArtifacts.requestNetworkRecords = _ => Promise.resolve(networkRecords);
computedArtifacts.URL = {finalUrl: 'https://m.example.com'};

return computedArtifacts.requestMainResource({}).then(() => {
assert.ok(false, 'should have thrown');
Expand All @@ -46,28 +47,9 @@ describe('MainResource computed artifact', () => {
});
});

it('should ignore redirects', () => {
const record = {
statusCode: 404,
};
const networkRecords = [
{
statusCode: 301,
},
{
statusCode: 302,
},
record,
];
computedArtifacts.requestNetworkRecords = _ => Promise.resolve(networkRecords);

return computedArtifacts.requestMainResource({}).then(output => {
assert.equal(output, record);
});
});

it('should identify correct main resource in the wikipedia fixture', () => {
const wikiDevtoolsLog = require('../../fixtures/wikipedia-redirect.devtoolslog.json');
computedArtifacts.URL = {finalUrl: 'https://en.m.wikipedia.org/wiki/Main_Page'};

return computedArtifacts.requestMainResource(wikiDevtoolsLog).then(output => {
assert.equal(output.url, 'https://en.m.wikipedia.org/wiki/Main_Page');
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/test/gather/fake-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
return Promise.resolve();
},
gotoURL() {
return Promise.resolve('https://example.com');
return Promise.resolve('https://www.reddit.com/r/nba');
Copy link
Collaborator Author

@kdzwinel kdzwinel Feb 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's the correct URL for the ../fixtures/traces/progressive-app.json returned by the fake-driver

},
waitForLoadEvent() {
return Promise.resolve();
Expand Down
5 changes: 4 additions & 1 deletion lighthouse-core/test/runner-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ describe('Runner', () => {
],

artifacts: {
URL: {
finalUrl: 'https://www.reddit.com/r/nba',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical-request-chains now depends on URL via mainResource

},
devtoolsLogs: {
defaultPass: path.join(__dirname, '/fixtures/perflog.json'),
},
Expand Down Expand Up @@ -473,7 +476,7 @@ describe('Runner', () => {
const config = new Config({
passes: [{
passName: 'firstPass',
gatherers: ['viewport-dimensions'],
gatherers: ['url', 'viewport-dimensions'],
}],

audits: [
Expand Down