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(uses-rel-preconnect): add FCP and LCP savings #15281

Merged
merged 7 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 11 additions & 11 deletions core/audits/uses-rel-preconnect.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@
const devtoolsLog = artifacts.devtoolsLogs[UsesRelPreconnectAudit.DEFAULT_PASS];
const settings = context.settings;

let maxWasted = 0;
let maxWastedFCP = 0;
let maxWastedLcp = 0;
let maxWastedFcp = 0;
/** @type {Array<LH.IcuMessage>} */
const warnings = [];

Expand Down Expand Up @@ -225,11 +225,11 @@
return;
}

maxWasted = Math.max(wastedMs, maxWasted);
maxWastedLcp = Math.max(wastedMs, maxWastedLcp);

if (fcpGraphURLs.has(firstRecordOfOrigin.url)) {
maxWastedFCP = Math.max(wastedMs, maxWasted);
maxWastedFcp = Math.max(wastedMs, maxWastedLcp);
}

Check warning on line 232 in core/audits/uses-rel-preconnect.js

View check run for this annotation

Codecov / codecov/patch

core/audits/uses-rel-preconnect.js#L231-L232

Added lines #L231 - L232 were not covered by tests
results.push({
url: securityOrigin,
wastedMs: wastedMs,
Expand All @@ -253,7 +253,7 @@
score: 1,
warnings: preconnectLinks.length >= 3 ?
[...warnings, str_(UIStrings.tooManyPreconnectLinksWarning)] : warnings,
metricSavings: {LCP: maxWasted, FCP: maxWastedFCP},
metricSavings: {LCP: maxWastedLcp, FCP: maxWastedFcp},
};
}

Expand All @@ -264,18 +264,18 @@
];

const details = Audit.makeOpportunityDetails(headings, results,
{overallSavingsMs: maxWasted, sortedBy: ['wastedMs']});
{overallSavingsMs: maxWastedLcp, sortedBy: ['wastedMs']});

return {
score: ByteEfficiencyAudit.scoreForWastedMs(maxWasted),
numericValue: maxWasted,
score: ByteEfficiencyAudit.scoreForWastedMs(maxWastedLcp),
numericValue: maxWastedLcp,
numericUnit: 'millisecond',
displayValue: maxWasted ?
str_(i18n.UIStrings.displayValueMsSavings, {wastedMs: maxWasted}) :
displayValue: maxWastedLcp ?
str_(i18n.UIStrings.displayValueMsSavings, {wastedMs: maxWastedLcp}) :
'',
warnings,
details,
metricSavings: {LCP: maxWasted, FCP: maxWastedFCP},
metricSavings: {LCP: maxWastedLcp, FCP: maxWastedFcp},
};
}
}
Expand Down
47 changes: 44 additions & 3 deletions core/test/audits/uses-rel-preconnect-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ const mainResource = {
priority: 'High',
};

function buildArtifacts(networkRecords) {
function buildArtifacts(networkRecords, fcpTs) {
const trace = createTestTrace({
timeOrigin: 0,
largestContentfulPaint: 5000,
firstContentfulPaint: 5000,
firstContentfulPaint: fcpTs ? fcpTs : 5000,
topLevelTasks: [{ts: 1000, duration: 50}],
});
const devtoolsLog = networkRecordsToDevtoolsLog(networkRecords);
Expand Down Expand Up @@ -197,7 +197,8 @@ describe('Performance: uses-rel-preconnect audit', () => {

const artifacts = buildArtifacts(networkRecords);
const context = {settings: {}, computedCache: new Map()};
const {numericValue, details, metricSavings} = await UsesRelPreconnect.audit(artifacts, context);
const {numericValue, details, metricSavings} =
await UsesRelPreconnect.audit(artifacts, context);
assert.equal(numericValue, 300);
assert.equal(details.items.length, 1);
assert.deepStrictEqual(details.items, [
Expand Down Expand Up @@ -353,4 +354,44 @@ describe('Performance: uses-rel-preconnect audit', () => {
assert.equal(result.score, 1);
assert.equal(result.warnings.length, 1);
});

it(`should have LCP savings and not FCP savings `, async () => {
adrianaixba marked this conversation as resolved.
Show resolved Hide resolved
const networkRecords = [
mainResource,
{
url: 'https://cdn.example.com/first',
initiator: {},
networkRequestTime: 3000,
timing: {
dnsStart: 300,
connectStart: 350,
connectEnd: 400,
},
priority: 'High',
},
{
url: 'https://cdn.example.com/second',
initiator: {},
networkRequestTime: 4000,
adrianaixba marked this conversation as resolved.
Show resolved Hide resolved
networkEndTime: 4500, // ends *before* LCP
timing: {
dnsStart: 100,
connectStart: 200,
connectEnd: 300,
},
priority: 'High',
},
];

const artifacts = buildArtifacts(networkRecords, 4000);
const context = {settings: {}, computedCache: new Map()};
const {numericValue, details, metricSavings} =
await UsesRelPreconnect.audit(artifacts, context);
assert.equal(numericValue, 300);
assert.equal(details.items.length, 1);
assert.deepStrictEqual(details.items, [
{url: 'https://cdn.example.com', wastedMs: 300},
]);
assert.deepStrictEqual(metricSavings, {LCP: 300, FCP: 0});
});
});
Loading