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(largest-contentful-paint-element): add LCP savings #15178

Merged
merged 6 commits into from
Aug 15, 2023
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
25 changes: 21 additions & 4 deletions core/audits/largest-contentful-paint-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import {Audit} from './audit.js';
import * as i18n from '../lib/i18n/i18n.js';
import {LargestContentfulPaint} from '../computed/metrics/largest-contentful-paint.js';
import {LargestContentfulPaint as LargestContentfulPaintComputed} from '../computed/metrics/largest-contentful-paint.js';
import LargestContentfulPaint from './metrics/largest-contentful-paint.js';
import {LCPBreakdown} from '../computed/metrics/lcp-breakdown.js';
import {Sentry} from '../lib/sentry.js';

Expand Down Expand Up @@ -124,14 +125,22 @@
settings: context.settings, URL: artifacts.URL};

const elementTable = this.makeElementTable(artifacts);
if (!elementTable) return {score: null, notApplicable: true};
if (!elementTable) {
return {
score: null,
notApplicable: true,
metricSavings: {LCP: 0},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've generally been of the mindset that we should avoid adding any of the usual stuff to audits in the notApplicable case. It's not a hard and fast rule, but when looking at an LHR, you generally shouldn't have to wonder if a value on an audit is important. The whole thing has been marked not applicable; it's not that there's no savings to be had, it's that this audit is irrelevant for this particular page load.

(the only exception might be debugdata if we ever want to pipe through information on why something was marked n/a to http archive or wherever, but I don't know if that's ever come up before)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but I was planning for metricSavings to eventually replace our current metric applicability lists. Removing metricSavings when an audit is N/A means that it will always be hidden when the user filters to any metric.

Not necessarily a bad UX, but it's also not how we do the filters right now. Knowing that an audit can affect a certain metric even when it's N/A can be useful information IMO.

};
}

Check warning on line 134 in core/audits/largest-contentful-paint-element.js

View check run for this annotation

Codecov / codecov/patch

core/audits/largest-contentful-paint-element.js#L129-L134

Added lines #L129 - L134 were not covered by tests

const items = [elementTable];
let displayValue;
let metricLcp = 0;

try {
const {timing: metricLcp} =
await LargestContentfulPaint.request(metricComputationData, context);
const lcpResult =
await LargestContentfulPaintComputed.request(metricComputationData, context);
metricLcp = lcpResult.timing;
displayValue = str_(i18n.UIStrings.ms, {timeInMs: metricLcp});

const phaseTable = await this.makePhaseTable(metricLcp, metricComputationData, context);
Expand All @@ -146,10 +155,18 @@

const details = Audit.makeListDetails(items);

// Conceptually, this doesn't make much sense as "savings" for this audit since there isn't anything to "fix".
// However, this audit will always be useful when improving LCP and that should be reflected in our impact calculations.
const idealLcp = LargestContentfulPaint.defaultOptions[context.settings.formFactor].scoring.p10;
const lcpSavings = Math.max(0, metricLcp - idealLcp);

return {
score: 1,
displayValue,
details,
metricSavings: {
LCP: lcpSavings,
},
};
}
}
Expand Down
2 changes: 2 additions & 0 deletions core/test/audits/largest-contentful-paint-element-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ describe('Performance: largest-contentful-paint-element audit', () => {
expect(auditResult.score).toEqual(1);
expect(auditResult.notApplicable).toBeUndefined();
expect(auditResult.displayValue).toBeDisplayString('5,800\xa0ms');
expect(auditResult.metricSavings).toEqual({LCP: 3304}); // 5804 - 2500 (p10 mobile)
expect(auditResult.details.items).toHaveLength(2);
expect(auditResult.details.items[0].items).toHaveLength(1);
expect(auditResult.details.items[0].items[0].node.path).toEqual('1,HTML,3,BODY,5,DIV,0,HEADER');
Expand Down Expand Up @@ -148,6 +149,7 @@ describe('Performance: largest-contentful-paint-element audit', () => {
expect(auditResult.score).toEqual(null);
expect(auditResult.notApplicable).toEqual(true);
expect(auditResult.displayValue).toBeUndefined();
expect(auditResult.metricSavings).toEqual({LCP: 0});
expect(auditResult.details).toBeUndefined();
});

Expand Down
Loading