Skip to content

Commit

Permalink
core(third-party-summary): don't consider main resource entity to be …
Browse files Browse the repository at this point in the history
…third party (GoogleChrome#10006)
  • Loading branch information
mikedijkstra authored and patrickhulce committed Nov 26, 2019
1 parent 6c44281 commit 105f30d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lighthouse-core/audits/third-party-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Audit = require('./audit.js');
const BootupTime = require('./bootup-time.js');
const i18n = require('../lib/i18n/i18n.js');
const NetworkRecords = require('../computed/network-records.js');
const MainResource = require('../computed/main-resource.js');
const MainThreadTasks = require('../computed/main-thread-tasks.js');

const UIStrings = {
Expand Down Expand Up @@ -48,7 +49,7 @@ class ThirdPartySummary extends Audit {
title: str_(UIStrings.title),
failureTitle: str_(UIStrings.failureTitle),
description: str_(UIStrings.description),
requiredArtifacts: ['traces', 'devtoolsLogs'],
requiredArtifacts: ['traces', 'devtoolsLogs', 'URL'],
};
}

Expand Down Expand Up @@ -119,6 +120,8 @@ class ThirdPartySummary extends Audit {
const trace = artifacts.traces[Audit.DEFAULT_PASS];
const devtoolsLog = artifacts.devtoolsLogs[Audit.DEFAULT_PASS];
const networkRecords = await NetworkRecords.request(devtoolsLog, context);
const mainResource = await MainResource.request({devtoolsLog, URL: artifacts.URL}, context);
const mainEntity = ThirdPartySummary.getEntitySafe(mainResource.url);
const tasks = await MainThreadTasks.request(trace, context);
const multiplier = settings.throttlingMethod === 'simulate' ?
settings.throttling.cpuSlowdownMultiplier : 1;
Expand All @@ -128,6 +131,9 @@ class ThirdPartySummary extends Audit {
const summary = {wastedBytes: 0, wastedMs: 0};

const results = Array.from(summaryByEntity.entries())
// Don't consider the page we're on to be third-party.
// e.g. Facebook SDK isn't a third-party script on facebook.com
.filter(([entity]) => !(mainEntity && mainEntity.name === entity.name))
.map(([entity, stats]) => {
summary.wastedBytes += stats.transferSize;
summary.wastedMs += stats.blockingTime;
Expand Down
36 changes: 36 additions & 0 deletions lighthouse-core/test/audits/third-party-summary-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('Third party summary', () => {
const artifacts = {
devtoolsLogs: {defaultPass: pwaDevtoolsLog},
traces: {defaultPass: pwaTrace},
URL: {finalUrl: 'https://pwa-rocks.com'},
};

const results = await ThirdPartySummary.audit(artifacts, {computedCache: new Map()});
Expand Down Expand Up @@ -54,6 +55,7 @@ describe('Third party summary', () => {
const artifacts = {
devtoolsLogs: {defaultPass: pwaDevtoolsLog},
traces: {defaultPass: pwaTrace},
URL: {finalUrl: 'https://pwa-rocks.com'},
};

const settings = {throttlingMethod: 'simulate', throttling: {cpuSlowdownMultiplier: 4}};
Expand All @@ -71,6 +73,7 @@ describe('Third party summary', () => {
const artifacts = {
devtoolsLogs: {defaultPass: networkRecordsToDevtoolsLog([{url: 'chrome://version'}])},
traces: {defaultPass: noThirdPartyTrace},
URL: {finalUrl: 'chrome://version'},
};

const settings = {throttlingMethod: 'simulate', throttling: {cpuSlowdownMultiplier: 4}};
Expand All @@ -81,4 +84,37 @@ describe('Third party summary', () => {
notApplicable: true,
});
});

it('does not return third party entity that matches main resource entity', async () => {
const externalArtifacts = {
devtoolsLogs: {
defaultPass: networkRecordsToDevtoolsLog([
{url: 'http://example.com'},
{url: 'http://photos-c.ak.fbcdn.net/photos-ak-sf2p/photo.jpg'},
]),
},
traces: {defaultPass: pwaTrace},
URL: {finalUrl: 'http://example.com'},
};
const facebookArtifacts = {
devtoolsLogs: {
defaultPass: networkRecordsToDevtoolsLog([
{url: 'http://facebook.com'},
{url: 'http://photos-c.ak.fbcdn.net/photos-ak-sf2p/photo.jpg'},
]),
},
traces: {defaultPass: pwaTrace},
URL: {finalUrl: 'http://facebook.com'},
};
const context = {computedCache: new Map()};

const resultsOnExternal = await ThirdPartySummary.audit(externalArtifacts, context);
const resultsOnFacebook = await ThirdPartySummary.audit(facebookArtifacts, context);

const externalEntities = resultsOnExternal.details.items.map(item => item.entity.text);
const facebookEntities = resultsOnFacebook.details.items.map(item => item.entity.text);

expect(externalEntities).toEqual(['Google Tag Manager', 'Facebook', 'Google Analytics']);
expect(facebookEntities).toEqual(['Google Tag Manager', 'Google Analytics']);
});
});

0 comments on commit 105f30d

Please sign in to comment.