Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(replay): Add layout shift to CLS replay data#13386
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
f69ad3c47b9d8c46d2fc8ccc3f9e3c4ac43d2281073bc50089a01f96505afebb5cc4f0bde59a9d9a4bb7ee1c9ef2afdbb14d996d26fd3b0c5ba269cFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -43,7 +43,13 @@ export interface Metric { | ||
| * The array may also be empty if the metric value was not based on any | ||
| * entries (e.g. a CLS value of 0 given no layout shifts). | ||
| */ | ||
| entries: PerformanceEntry[] | PerformanceEventTiming[]; | ||
| entries: PerformanceEntry[] | LayoutShift[]; | ||
| } | ||
| interface LayoutShift extends PerformanceEntry { | ||
| value: number; | ||
| sources: LayoutShiftAttribution[]; | ||
| hadRecentInput: boolean; | ||
billyvg marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| interface LayoutShiftAttribution { | ||
| @@ -52,6 +58,11 @@ interface LayoutShiftAttribution { | ||
| currentRect: DOMRectReadOnly; | ||
| } | ||
| interface Attribution { | ||
| value: number; | ||
| nodeIds?: number[]; | ||
| } | ||
| /** | ||
| * Handler creater for web vitals | ||
| */ | ||
| @@ -187,22 +198,32 @@ export function getLargestContentfulPaint(metric: Metric): ReplayPerformanceEntr | ||
| return getWebVital(metric, 'largest-contentful-paint', node); | ||
| } | ||
| function isLayoutShift(entry: PerformanceEntry | LayoutShift): entry is LayoutShift { | ||
| return (entry as LayoutShift).sources !== undefined; | ||
| } | ||
| /** | ||
| * Add a CLS event to the replay based on a CLS metric. | ||
| */ | ||
| export function getCumulativeLayoutShift(metric: Metric): ReplayPerformanceEntry<WebVitalData> { | ||
| const lastEntry = metric.entries[metric.entries.length - 1] as | ||
| | (PerformanceEntry & { sources?: LayoutShiftAttribution[] }) | ||
| | undefined; | ||
| const layoutShifts: Attribution[] = []; | ||
| const nodes: Node[] = []; | ||
| if (lastEntry && lastEntry.sources) { | ||
| for (const source of lastEntry.sources) { | ||
| if (source.node) { | ||
| nodes.push(source.node); | ||
| for (const entry of metric.entries) { | ||
| if (isLayoutShift(entry)) { | ||
| const nodeIds = []; | ||
| for (const source of entry.sources) { | ||
| if (source.node) { | ||
| nodes.push(source.node); | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nodes is kind of duplicated here since the nodeIds will be in ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea we currently need nodeIds in the new frontend to get the selector | ||
| const nodeId = record.mirror.getId(source.node); | ||
| if (nodeId) { | ||
| nodeIds.push(nodeId); | ||
| } | ||
| } | ||
| } | ||
| layoutShifts.push({ value: entry.value, nodeIds }); | ||
| } | ||
| } | ||
| return getWebVital(metric, 'cumulative-layout-shift', nodes); | ||
| return getWebVital(metric, 'cumulative-layout-shift', nodes, layoutShifts); | ||
| } | ||
| /** | ||
| @@ -226,7 +247,12 @@ export function getInteractionToNextPaint(metric: Metric): ReplayPerformanceEntr | ||
| /** | ||
| * Add an web vital event to the replay based on the web vital metric. | ||
| */ | ||
| function getWebVital(metric: Metric, name: string, nodes: Node[] | undefined): ReplayPerformanceEntry<WebVitalData> { | ||
| function getWebVital( | ||
| metric: Metric, | ||
| name: string, | ||
| nodes: Node[] | undefined, | ||
| attributions?: Attribution[], | ||
| ): ReplayPerformanceEntry<WebVitalData> { | ||
| const value = metric.value; | ||
| const rating = metric.rating; | ||
| @@ -242,6 +268,7 @@ function getWebVital(metric: Metric, name: string, nodes: Node[] | undefined): R | ||
| size: value, | ||
| rating, | ||
| nodeIds: nodes ? nodes.map(node => record.mirror.getId(node)) : undefined, | ||
| attributions, | ||
| }, | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.