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(third-party-summary): check main domain against all third party domains #10006

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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))
mikedijkstra marked this conversation as resolved.
Show resolved Hide resolved
.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']);
});
});