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 1 commit
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
23 changes: 11 additions & 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,17 @@ class MainResource extends ComputedArtifact {
compute_(devtoolsLog, artifacts) {
return artifacts.requestNetworkRecords(devtoolsLog)
.then(requests => {
const mainResource = requests.find(this.isMainResource);
let mainResource = null;

for (/** @type {WebInspector.NetworkRequest} */const request of requests) {
if (mainResource === null) {
mainResource = request;
}

if (request.redirectSource && request.redirectSource.url === mainResource.url) {
mainResource = request;
}
}

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

it('returns an artifact', () => {
const record = {
statusCode: 404,
url: 'https://example.com',
};
const networkRecords = [
record,
Expand All @@ -33,9 +33,6 @@ describe('MainResource computed artifact', () => {

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

Expand All @@ -48,16 +45,31 @@ describe('MainResource computed artifact', () => {

it('should ignore redirects', () => {
const record = {
statusCode: 404,
url: 'http://example.com/3',
redirectSource: {
url: 'http://example.com/2',
},
};
const networkRecords = [
{
statusCode: 301,
url: 'http://example.com/1',
},
{
statusCode: 302,
url: 'http://example.com/2',
redirectSource: {
url: 'http://example.com/1',
},
},
record,
{
url: 'http://example.com/someimage.jpg',
},
{
url: 'https://example.com/someimage.jpg',
redirectSource: {
url: 'http://example.com/someimage.jpg',
},
},
];
computedArtifacts.requestNetworkRecords = _ => Promise.resolve(networkRecords);

Expand Down