[go: nahoru, domu]

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

new_audit: lcp-lazy-loaded #12838

Merged
merged 18 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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: 8 additions & 0 deletions lighthouse-cli/test/cli/__snapshots__/index-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ Object {
Object {
"path": "largest-contentful-paint-element",
},
Object {
"path": "lcp-lazy-loaded",
},
Object {
"path": "layout-shift-elements",
},
Expand Down Expand Up @@ -1092,6 +1095,11 @@ Object {
"id": "largest-contentful-paint-element",
"weight": 0,
},
Object {
"group": "diagnostics",
"id": "lcp-lazy-loaded",
"weight": 0,
},
Object {
"group": "diagnostics",
"id": "layout-shift-elements",
Expand Down
88 changes: 88 additions & 0 deletions lighthouse-core/audits/lcp-lazy-loaded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @license Copyright 2021 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 Audit = require('./audit.js');
const i18n = require('../lib/i18n/i18n.js');

const UIStrings = {
/** Descriptive title of a diagnostic audit that provides */
title: 'Largest Contentful Paint element was lazy-loaded',
milutin marked this conversation as resolved.
Show resolved Hide resolved
/** Description of a Lighthouse audit that tells */
description: 'Consider to remove lazy loading for largest contentful paint element.',
milutin marked this conversation as resolved.
Show resolved Hide resolved
};

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

class LargestContentfulPaintLazyLoaded extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'lcp-lazy-loaded',
title: str_(UIStrings.title),
description: str_(UIStrings.description),
supportedModes: ['navigation'],
requiredArtifacts: ['TraceElements', 'ViewportDimensions', 'ImageElements'],
};
}

/**
* @param {LH.Artifacts.ImageElement} image
* @param {LH.Artifacts.ViewportDimensions} viewportDimensions
* @return {boolean}
*/
static isImageInViewport(image, viewportDimensions) {
const imageTop = image.clientRect.top;
const viewportHeight = viewportDimensions.innerHeight;
return imageTop < viewportHeight;
}

/**
* @param {LH.Artifacts} artifacts
* @return {LH.Audit.Product}
*/
static audit(artifacts) {
const images = artifacts.ImageElements;
const lazyLoadedImages = images.filter(
milutin marked this conversation as resolved.
Show resolved Hide resolved
image => image.loading === 'lazy');
const lcpElement = artifacts.TraceElements
.find(element => element.traceEventType === 'largest-contentful-paint');

const lcpElementDetails = [];
if (lcpElement) {
const lcpImageElement = lazyLoadedImages.find(elem => {
return elem.node.devtoolsNodePath === lcpElement.node.devtoolsNodePath
&& this.isImageInViewport(elem, artifacts.ViewportDimensions);
});
if (lcpImageElement) {
lcpElementDetails.push({
node: Audit.makeNodeItem(lcpImageElement.node),
});
}
}

if (lcpElementDetails.length === 0) {
return {score: 1, notApplicable: true};
}

/** @type {LH.Audit.Details.Table['headings']} */
const headings = [
{key: 'node', itemType: 'node', text: str_(i18n.UIStrings.columnElement)},
];

const details = Audit.makeTableDetails(headings, lcpElementDetails);

return {
score: 0,
details,
};
}
}

module.exports = LargestContentfulPaintLazyLoaded;
module.exports.UIStrings = UIStrings;
2 changes: 2 additions & 0 deletions lighthouse-core/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ const defaultConfig = {
'third-party-summary',
'third-party-facades',
'largest-contentful-paint-element',
'lcp-lazy-loaded',
'layout-shift-elements',
'long-tasks',
'no-unload-listeners',
Expand Down Expand Up @@ -466,6 +467,7 @@ const defaultConfig = {
{id: 'third-party-summary', weight: 0, group: 'diagnostics'},
{id: 'third-party-facades', weight: 0, group: 'diagnostics'},
{id: 'largest-contentful-paint-element', weight: 0, group: 'diagnostics'},
{id: 'lcp-lazy-loaded', weight: 0, group: 'diagnostics'},
{id: 'layout-shift-elements', weight: 0, group: 'diagnostics'},
{id: 'uses-passive-event-listeners', weight: 0, group: 'diagnostics'},
{id: 'no-document-write', weight: 0, group: 'diagnostics'},
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.

62 changes: 62 additions & 0 deletions lighthouse-core/test/audits/lcp-lazy-loaded-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @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 LargestContentfulPaintLazyLoaded =
require('../../audits/lcp-lazy-loaded.js');

/* eslint-env jest */
describe('Performance: lcp-lazy-loaded audit', () => {
it('correctly surfaces the lazy loaded LCP element', async () => {
milutin marked this conversation as resolved.
Show resolved Hide resolved
const node = {
devtoolsNodePath: '1,HTML,1,BODY,3,DIV,2,IMG',
selector: 'div.l-header > div.chorus-emc__content',
nodeLabel: 'My Test Label',
snippet: '<img class="test-class">',
};
const artifacts = {
TraceElements: [{
traceEventType: 'largest-contentful-paint',
node,
}],
ImageElements: [
{
src: 'test',
loading: 'lazy',
clientRect: {
top: 0,
bottom: 400,
left: 0,
right: 0,
},
node,
},
],
ViewportDimensions: {
innerHeight: 500,
innerWidth: 300,
},
};

const auditResult = await LargestContentfulPaintLazyLoaded.audit(artifacts);
expect(auditResult.score).toEqual(0);
expect(auditResult.details.items).toHaveLength(1);
expect(auditResult.details.items[0].node.path).toEqual('1,HTML,1,BODY,3,DIV,2,IMG');
expect(auditResult.details.items[0].node.nodeLabel).toEqual('My Test Label');
expect(auditResult.details.items[0].node.snippet).toEqual('<img class="test-class">');
});

it('doesn\'t throw an error when there is nothing to show', async () => {
const artifacts = {
TraceElements: [],
ImageElements: [],
};

const auditResult = await LargestContentfulPaintLazyLoaded.audit(artifacts);
expect(auditResult.score).toEqual(1);
expect(auditResult.notApplicable).toEqual(true);
});
});
2 changes: 1 addition & 1 deletion lighthouse-core/test/fraggle-rock/api-test-pptr.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ describe('Fraggle Rock API', () => {
const {lhr} = result;
const {auditResults, failedAudits, erroredAudits} = getAuditsBreakdown(lhr);
// TODO(FR-COMPAT): This assertion can be removed when full compatibility is reached.
expect(auditResults.length).toMatchInlineSnapshot(`153`);
expect(auditResults.length).toMatchInlineSnapshot(`154`);
expect(erroredAudits).toHaveLength(0);

const failedAuditIds = failedAudits.map(audit => audit.id);
Expand Down
24 changes: 24 additions & 0 deletions lighthouse-core/test/results/sample_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -1859,6 +1859,13 @@
]
}
},
"lcp-lazy-loaded": {
"id": "lcp-lazy-loaded",
"title": "Largest Contentful Paint element was lazy-loaded",
"description": "Consider to remove lazy loading for largest contentful paint element.",
"score": null,
"scoreDisplayMode": "notApplicable"
},
"layout-shift-elements": {
"id": "layout-shift-elements",
"title": "Avoid large layout shifts",
Expand Down Expand Up @@ -5188,6 +5195,11 @@
"weight": 0,
"group": "diagnostics"
},
{
"id": "lcp-lazy-loaded",
"weight": 0,
"group": "diagnostics"
},
{
"id": "layout-shift-elements",
"weight": 0,
Expand Down Expand Up @@ -6352,6 +6364,12 @@
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:lcp-lazy-loaded",
"duration": 100,
"entryType": "measure"
},
{
"startTime": 0,
"name": "lh:audit:layout-shift-elements",
Expand Down Expand Up @@ -7691,6 +7709,12 @@
"audits[non-composited-animations].details.headings[0].text",
"audits[dom-size].details.headings[1].text"
],
"lighthouse-core/audits/lcp-lazy-loaded.js | title": [
"audits[lcp-lazy-loaded].title"
],
"lighthouse-core/audits/lcp-lazy-loaded.js | description": [
"audits[lcp-lazy-loaded].description"
],
"lighthouse-core/audits/layout-shift-elements.js | title": [
"audits[layout-shift-elements].title"
],
Expand Down