Skip to content

Commit

Permalink
Merge 47f237f into fe28a1a
Browse files Browse the repository at this point in the history
  • Loading branch information
Beytoven committed Oct 27, 2020
2 parents fe28a1a + 47f237f commit a8dacbc
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 3 deletions.
176 changes: 176 additions & 0 deletions lighthouse-core/audits/preload-lcp-image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/**
* @license Copyright 2020 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';

const URL = require('../lib/url-shim.js');
const Audit = require('./audit.js');
const i18n = require('../lib/i18n/i18n.js');
const MainResource = require('../computed/main-resource.js');
const LanternLCP = require('../computed/metrics/lantern-largest-contentful-paint.js');
const LoadSimulator = require('../computed/load-simulator.js');
const UnusedBytes = require('./byte-efficiency/byte-efficiency-audit.js');

const UIStrings = {
/** Title of a lighthouse audit that tells a user to preload an image in order to improve their LCP time. */
title: 'Preload Largest Contentful Paint image',
/** Description of a lighthouse audit that tells a user to preload an image in order to improve their LCP time. */
description: 'Preload the image used by ' +
'the LCP element in order to improve your LCP time. [Learn more](https://web.dev/optimize-lcp/#preload-important-resources).',
};

const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);

class PreloadLCPImageAudit extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'preload-lcp-image',
title: str_(UIStrings.title),
description: str_(UIStrings.description),
requiredArtifacts: ['traces', 'devtoolsLogs', 'URL', 'TraceElements', 'ImageElements'],
scoreDisplayMode: Audit.SCORING_MODES.NUMERIC,
};
}

/**
*
* @param {LH.Artifacts.NetworkRequest} request
* @param {LH.Artifacts.NetworkRequest} mainResource
* @param {Array<LH.Gatherer.Simulation.GraphNode>} initiatorPath
* @return {boolean}
*/
static shouldPreloadRequest(request, mainResource, initiatorPath) {
const mainResourceDepth = mainResource.redirects ? mainResource.redirects.length : 0;

// If it's already preloaded, no need to recommend it.
if (request.isLinkPreload) return false;
// It's not a request loaded over the network, don't recommend it.
if (URL.NON_NETWORK_PROTOCOLS.includes(request.protocol)) return false;
// It's already discoverable from the main document, don't recommend it.
if (initiatorPath.length <= mainResourceDepth) return false;
// Finally, return whether or not it belongs to the main frame
return request.frameId === mainResource.frameId;
}

/**
* @param {LH.Gatherer.Simulation.GraphNode} graph
* @param {string} imageUrl
* @return {{lcpNode: LH.Gatherer.Simulation.GraphNetworkNode|undefined, path: Array<LH.Gatherer.Simulation.GraphNetworkNode>|undefined}}
*/
static findLCPNode(graph, imageUrl) {
let lcpNode;
let path;
graph.traverse((node, traversalPath) => {
if (node.type !== 'network') return;
if (node.record.url === imageUrl) {
lcpNode = node;
path =
traversalPath.slice(1).filter(initiator => initiator.type === 'network');
}
});
return {
lcpNode,
path,
};
}

/**
* @param {LH.Artifacts.NetworkRequest} mainResource
* @param {LH.Gatherer.Simulation.GraphNode} graph
* @param {LH.Artifacts.TraceElement|undefined} lcpElement
* @param {Array<LH.Artifacts.ImageElement>} imageElements
* @return {LH.Gatherer.Simulation.GraphNetworkNode|undefined}
*/
static getLCPNodeToPreload(mainResource, graph, lcpElement, imageElements) {
if (!lcpElement) return undefined;

const lcpImageElement = imageElements.find(elem => {
return elem.devtoolsNodePath === lcpElement.devtoolsNodePath;
});

if (!lcpImageElement) return undefined;
const lcpUrl = lcpImageElement.src;
const {lcpNode, path} = PreloadLCPImageAudit.findLCPNode(graph, lcpUrl);
if (!lcpNode || !path) return undefined;
// eslint-disable-next-line max-len
const shouldPreload = PreloadLCPImageAudit.shouldPreloadRequest(lcpNode.record, mainResource, path);
return shouldPreload ? lcpNode : undefined;
}

/**
* Computes the estimated effect of preloading the LCP image.
* @param {LH.Gatherer.Simulation.GraphNetworkNode} lcpNode
* @param {LH.Gatherer.Simulation.GraphNode} graph
* @param {LH.Gatherer.Simulation.Simulator} simulator
* @return {{wastedMs: number, results: Array<{url: string, wastedMs: number}>}}
*/
static computeWasteWithGraph(lcpNode, graph, simulator) {
const simulation = simulator.simulate(graph, {flexibleOrdering: true});
// For now, we are simply using the duration of the LCP image request as the wastedMS value
const lcpTiming = simulation.nodeTimings.get(lcpNode);
const wastedMs = lcpTiming && lcpTiming.duration || 0;
return {
wastedMs,
results: [{
url: lcpNode.record.url,
wastedMs,
}],
};
}

/**
* @param {LH.Artifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<LH.Audit.Product>}
*/
static async audit(artifacts, context) {
const trace = artifacts.traces[PreloadLCPImageAudit.DEFAULT_PASS];
const devtoolsLog = artifacts.devtoolsLogs[PreloadLCPImageAudit.DEFAULT_PASS];
const URL = artifacts.URL;
const simulatorOptions = {trace, devtoolsLog, settings: context.settings};
const lcpElement = artifacts.TraceElements
.find(element => element.traceEventType === 'largest-contentful-paint');

const [mainResource, lanternLCP, simulator] = await Promise.all([
MainResource.request({devtoolsLog, URL}, context),
LanternLCP.request(simulatorOptions, context),
LoadSimulator.request(simulatorOptions, context),
]);

const graph = lanternLCP.pessimisticGraph;
// eslint-disable-next-line max-len
const lcpNode = PreloadLCPImageAudit.getLCPNodeToPreload(mainResource, graph, lcpElement, artifacts.ImageElements);
if (!lcpNode) {
return {
score: 1,
notApplicable: true,
};
}

const {results, wastedMs} =
PreloadLCPImageAudit.computeWasteWithGraph(lcpNode, graph, simulator);

/** @type {LH.Audit.Details.Opportunity['headings']} */
const headings = [
{key: 'url', valueType: 'url', label: str_(i18n.UIStrings.columnURL)},
{key: 'wastedMs', valueType: 'timespanMs', label: str_(i18n.UIStrings.columnWastedMs)},
];
const details = Audit.makeOpportunityDetails(headings, results, wastedMs);

return {
score: UnusedBytes.scoreForWastedMs(wastedMs),
numericValue: wastedMs,
numericUnit: 'millisecond',
displayValue: wastedMs ? str_(i18n.UIStrings.displayValueMsSavings, {wastedMs}) : '',
details,
};
}
}

module.exports = PreloadLCPImageAudit;
module.exports.UIStrings = UIStrings;
2 changes: 2 additions & 0 deletions lighthouse-core/config/experimental-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const config = {
'full-page-screenshot',
'large-javascript-libraries',
'script-treemap-data',
'preload-lcp-image',
],
passes: [{
passName: 'defaultPass',
Expand All @@ -31,6 +32,7 @@ const config = {
'performance': {
auditRefs: [
{id: 'large-javascript-libraries', weight: 0, group: 'diagnostics'},
{id: 'preload-lcp-image', weight: 0, group: 'load-opportunities'},
],
},
// @ts-ignore: `title` is required in CategoryJson. setting to the same value as the default
Expand Down
6 changes: 6 additions & 0 deletions lighthouse-core/lib/i18n/locales/en-US.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lighthouse-core/lib/i18n/locales/en-XL.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lighthouse-core/lib/page-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ function getNodeLabel(node) {

/**
* @param {HTMLElement} element
* @param {LH.Artifacts.Rect}
* @return {LH.Artifacts.Rect}
*/
/* istanbul ignore next */
function getBoundingClientRect(element) {
Expand Down
135 changes: 135 additions & 0 deletions lighthouse-core/test/audits/preload-lcp-image-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/**
* @license Copyright 2020 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

'use strict';

/* eslint-env jest */

const PreloadLCPImage = require('../../audits/preload-lcp-image.js');
const networkRecordsToDevtoolsLog = require('../network-records-to-devtools-log.js');
const createTestTrace = require('../create-test-trace.js');

const rootNodeUrl = 'http://example.com:3000';
const mainDocumentNodeUrl = 'http://www.example.com:3000';
const scriptNodeUrl = 'http://www.example.com/script.js';
const imageUrl = 'http://www.example.com/image.png';

describe('Performance: preload-lcp audit', () => {
const mockArtifacts = (networkRecords, finalUrl, imageUrl) => {
return {
traces: {
[PreloadLCPImage.DEFAULT_PASS]: createTestTrace({
traceEnd: 6e3,
largestContentfulPaint: 45e2,
}),
},
devtoolsLogs: {[PreloadLCPImage.DEFAULT_PASS]: networkRecordsToDevtoolsLog(networkRecords)},
URL: {finalUrl},
TraceElements: [
{
traceEventType: 'largest-contentful-paint',
devtoolsNodePath: '1,HTML,1,BODY,3,DIV,2,IMG',
},
],
ImageElements: [
{
src: imageUrl,
devtoolsNodePath: '1,HTML,1,BODY,3,DIV,2,IMG',
},
],
};
};

const mockNetworkRecords = () => {
return [
{
requestId: '2',
priority: 'High',
isLinkPreload: false,
startTime: 0,
endTime: 0.5,
timing: {receiveHeadersEnd: 500},
url: rootNodeUrl,
},
{
requestId: '2:redirect',
resourceType: 'Document',
priority: 'High',
isLinkPreload: false,
startTime: 0.5,
endTime: 1,
timing: {receiveHeadersEnd: 500},
url: mainDocumentNodeUrl,
},
{
requestId: '3',
resourceType: 'Script',
priority: 'High',
isLinkPreload: false,
startTime: 1,
endTime: 5,
timing: {receiveHeadersEnd: 4000},
url: scriptNodeUrl,
initiator: {type: 'parser', url: mainDocumentNodeUrl},
},
{
requestId: '4',
resourceType: 'Image',
priority: 'High',
isLinkPreload: false,
startTime: 2,
endTime: 4.5,
timing: {receiveHeadersEnd: 2500},
url: imageUrl,
initiator: {type: 'script', url: scriptNodeUrl},
},
];
};

it('should suggest preloading a lcp image', async () => {
const networkRecords = mockNetworkRecords();
const artifacts = mockArtifacts(networkRecords, mainDocumentNodeUrl, imageUrl);
const context = {settings: {}, computedCache: new Map()};
const results = await PreloadLCPImage.audit(artifacts, context);
expect(results.numericValue).toEqual(180);
expect(results.details.overallSavingsMs).toEqual(180);
expect(results.details.items[0].url).toEqual(imageUrl);
expect(results.details.items[0].wastedMs).toEqual(180);
});

it('shouldn\'t be applicable if lcp image is not found', async () => {
const networkRecords = mockNetworkRecords();
const artifacts = mockArtifacts(networkRecords, mainDocumentNodeUrl, imageUrl);
artifacts.ImageElements = [];
const context = {settings: {}, computedCache: new Map()};
const results = await PreloadLCPImage.audit(artifacts, context);
expect(results.score).toEqual(1);
expect(results.notApplicable).toBeTruthy();
expect(results.details).toBeUndefined();
});

it('shouldn\'t be applicable if the lcp is already preloaded', async () => {
const networkRecords = mockNetworkRecords();
networkRecords[3].isLinkPreload = true;
const artifacts = mockArtifacts(networkRecords, mainDocumentNodeUrl, imageUrl);
const context = {settings: {}, computedCache: new Map()};
const results = await PreloadLCPImage.audit(artifacts, context);
expect(results.score).toEqual(1);
expect(results.notApplicable).toBeTruthy();
expect(results.details).toBeUndefined();
});

it('shouldn\'t be applicable if the lcp request is not from over the network', async () => {
const networkRecords = mockNetworkRecords();
networkRecords[3].protocol = 'data';
const artifacts = mockArtifacts(networkRecords, mainDocumentNodeUrl, imageUrl);
const context = {settings: {}, computedCache: new Map()};
const results = await PreloadLCPImage.audit(artifacts, context);
expect(results.score).toEqual(1);
expect(results.notApplicable).toBeTruthy();
expect(results.details).toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion lighthouse-core/test/audits/uses-rel-preconnect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const mainResource = {
function buildArtifacts(networkRecords) {
const trace = createTestTrace({
timeOrigin: 0,
largestContentfulPaint: 5000e3,
largestContentfulPaint: 5000,
topLevelTasks: [{ts: 1000, duration: 50}],
});
const devtoolsLog = networkRecordsToDevtoolsLog(networkRecords);
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/test/create-test-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function createTestTrace(options) {
if (options.largestContentfulPaint) {
traceEvents.push({
name: 'largestContentfulPaint::Candidate',
ts: options.largestContentfulPaint,
ts: options.largestContentfulPaint * 1000,
pid,
tid,
ph: 'R',
Expand Down

0 comments on commit a8dacbc

Please sign in to comment.