-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30b7e6b
commit 1461e63
Showing
16 changed files
with
2,299 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/** | ||
* @license Copyright 2023 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import {Audit} from './audit.js'; | ||
import * as i18n from '../lib/i18n/i18n.js'; | ||
import {CumulativeLayoutShift as CumulativeLayoutShiftComputed} from '../computed/metrics/cumulative-layout-shift.js'; | ||
import CumulativeLayoutShift from './metrics/cumulative-layout-shift.js'; | ||
import TraceElements from '../gather/gatherers/trace-elements.js'; | ||
|
||
/** @typedef {LH.Audit.Details.TableItem & {node?: LH.Audit.Details.NodeValue, score: number, subItems?: {type: 'subitems', items: SubItem[]}}} Item */ | ||
/** @typedef {{node?: LH.Audit.Details.NodeValue, cause: LH.IcuMessage}} SubItem */ | ||
|
||
const UIStrings = { | ||
/** Descriptive title of a diagnostic audit that provides the top elements affected by layout shifts. */ | ||
title: 'Avoid large layout shifts', | ||
/** Description of a diagnostic audit that provides the top elements affected by layout shifts. "windowing" means the metric value is calculated using the subset of events in a small window of time during the run. "normalization" is a good substitute for "windowing". The last sentence starting with 'Learn' becomes link text to additional documentation. */ | ||
description: 'These are the largest layout shifts observed on the page. Some layout shifts may not be included in the CLS metric value due to [windowing](https://web.dev/articles/cls#what_is_cls). [Learn how to improve CLS](https://web.dev/articles/optimize-cls)', | ||
/** Label for a column in a data table; entries in this column will be a number representing how large the layout shift was. */ | ||
columnScore: 'Layout shift score', | ||
/** A possible reason why that the layout shift occured. */ | ||
rootCauseUnsizedMedia: 'The size of an element changed when CSS was applied', | ||
}; | ||
|
||
const str_ = i18n.createIcuMessageFn(import.meta.url, UIStrings); | ||
|
||
class LayoutShifts extends Audit { | ||
/** | ||
* @return {LH.Audit.Meta} | ||
*/ | ||
static get meta() { | ||
return { | ||
id: 'layout-shifts', | ||
title: str_(UIStrings.title), | ||
description: str_(UIStrings.description), | ||
scoreDisplayMode: Audit.SCORING_MODES.METRIC_SAVINGS, | ||
guidanceLevel: 2, | ||
requiredArtifacts: ['traces', 'TraceElements'], | ||
}; | ||
} | ||
|
||
/** | ||
* @param {LH.Artifacts} artifacts | ||
* @param {LH.Audit.Context} context | ||
* @return {Promise<LH.Audit.Product>} | ||
*/ | ||
static async audit(artifacts, context) { | ||
const trace = artifacts.traces[Audit.DEFAULT_PASS]; | ||
const clusters = trace.traceEngineResult?.data.LayoutShifts.clusters ?? []; | ||
const {cumulativeLayoutShift: clsSavings, impactByNodeId} = | ||
await CumulativeLayoutShiftComputed.request(trace, context); | ||
|
||
/** @type {Item[]} */ | ||
const items = []; | ||
const layoutShiftEvents = clusters.flatMap(c => c.events); | ||
for (const event of layoutShiftEvents) { | ||
const biggestImpactNodeId = TraceElements.getBiggestImpactNodeForShiftEvent( | ||
event.args.data.impacted_nodes, impactByNodeId); | ||
const biggestImpactElement = artifacts.TraceElements.find( | ||
t => t.traceEventType === 'layout-shift' && t.nodeId === biggestImpactNodeId); | ||
|
||
// Turn root causes into sub-items. | ||
const index = layoutShiftEvents.indexOf(event); | ||
const rootCauses = trace.traceEngineResult?.rootCauses.layoutShifts[index]; | ||
/** @type {SubItem[]} */ | ||
const subItems = []; | ||
if (rootCauses) { | ||
// TODO ! finish these | ||
for (const cause of rootCauses.fontChanges) { | ||
// subItems.push({cause: JSON.stringify(cause)}); | ||
} | ||
for (const cause of rootCauses.iframes) { | ||
// subItems.push({cause: JSON.stringify(cause)}); | ||
} | ||
for (const cause of rootCauses.renderBlockingRequests) { | ||
// subItems.push({cause: JSON.stringify(cause)}); | ||
} | ||
for (const cause of rootCauses.unsizedMedia) { | ||
const unsizedMediaElement = artifacts.TraceElements.find( | ||
t => t.traceEventType === 'layout-shift' && t.nodeId === cause.node.backendNodeId); | ||
subItems.push({ | ||
node: unsizedMediaElement ? Audit.makeNodeItem(unsizedMediaElement.node) : undefined, | ||
cause: str_(UIStrings.rootCauseUnsizedMedia), | ||
}); | ||
} | ||
} | ||
|
||
items.push({ | ||
node: biggestImpactElement ? Audit.makeNodeItem(biggestImpactElement.node) : undefined, | ||
score: event.args.data.weighted_score_delta, | ||
subItems: subItems.length ? {type: 'subitems', items: subItems} : undefined, | ||
}); | ||
} | ||
|
||
/** @type {LH.Audit.Details.Table['headings']} */ | ||
const headings = [ | ||
{key: 'node', valueType: 'node', label: str_(i18n.UIStrings.columnElement)}, | ||
{key: 'score', valueType: 'numeric', granularity: 0.001, label: str_(UIStrings.columnScore)}, | ||
]; | ||
|
||
const details = Audit.makeTableDetails(headings, items); | ||
|
||
let displayValue; | ||
if (items.length > 0) { | ||
displayValue = str_(i18n.UIStrings.displayValueElementsFound, | ||
{nodeCount: items.length}); | ||
} | ||
|
||
const passed = clsSavings <= CumulativeLayoutShift.defaultOptions.p10; | ||
|
||
return { | ||
score: passed ? 1 : 0, | ||
scoreDisplayMode: passed ? Audit.SCORING_MODES.INFORMATIVE : undefined, | ||
metricSavings: { | ||
CLS: clsSavings, | ||
}, | ||
notApplicable: details.items.length === 0, | ||
displayValue, | ||
details, | ||
}; | ||
} | ||
} | ||
|
||
export default LayoutShifts; | ||
export {UIStrings}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.