diff --git a/lighthouse-core/gather/computed/main-resource.js b/lighthouse-core/gather/computed/main-resource.js index 3c59eec07704..660938722624 100644 --- a/lighthouse-core/gather/computed/main-resource.js +++ b/lighthouse-core/gather/computed/main-resource.js @@ -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 @@ -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 @@ -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'); diff --git a/lighthouse-core/test/gather/computed/critical-request-chains-test.js b/lighthouse-core/test/gather/computed/critical-request-chains-test.js index f0fd81d79e98..3b8f85d77454 100644 --- a/lighthouse-core/test/gather/computed/critical-request-chains-test.js +++ b/lighthouse-core/test/gather/computed/critical-request-chains-test.js @@ -50,6 +50,7 @@ function replaceChain(chains, networkRecords) { describe('CriticalRequestChain gatherer: extractChain function', () => { it('returns correct data for chain from a devtoolsLog', () => { const computedArtifacts = Runner.instantiateComputedArtifacts(); + computedArtifacts.URL = {finalUrl: 'https://en.m.wikipedia.org/wiki/Main_Page'}; const wikiDevtoolsLog = require('../../fixtures/wikipedia-redirect.devtoolslog.json'); const wikiChains = require('../../fixtures/wikipedia-redirect.critical-request-chains.json'); diff --git a/lighthouse-core/test/gather/computed/main-resource-test.js b/lighthouse-core/test/gather/computed/main-resource-test.js index a8c745d72255..854e204f6147 100644 --- a/lighthouse-core/test/gather/computed/main-resource-test.js +++ b/lighthouse-core/test/gather/computed/main-resource-test.js @@ -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); @@ -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'); @@ -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'); diff --git a/lighthouse-core/test/gather/fake-driver.js b/lighthouse-core/test/gather/fake-driver.js index e639daa33487..69bf1bd0772a 100644 --- a/lighthouse-core/test/gather/fake-driver.js +++ b/lighthouse-core/test/gather/fake-driver.js @@ -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'); }, waitForLoadEvent() { return Promise.resolve(); diff --git a/lighthouse-core/test/runner-test.js b/lighthouse-core/test/runner-test.js index 62e62d82a0f3..4d29e5a830f6 100644 --- a/lighthouse-core/test/runner-test.js +++ b/lighthouse-core/test/runner-test.js @@ -330,6 +330,9 @@ describe('Runner', () => { ], artifacts: { + URL: { + finalUrl: 'https://www.reddit.com/r/nba', + }, devtoolsLogs: { defaultPass: path.join(__dirname, '/fixtures/perflog.json'), }, @@ -473,7 +476,7 @@ describe('Runner', () => { const config = new Config({ passes: [{ passName: 'firstPass', - gatherers: ['viewport-dimensions'], + gatherers: ['url', 'viewport-dimensions'], }], audits: [