-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from 15 commits
f69ad3c
47b9d8c
46d2fc8
ccc3f9e
3c4ac43
d228107
3bc5008
9a01f96
505afeb
b5cc4f0
bde59a9
d9a4bb7
ee1c9ef
2afdbb1
4d996d2
6fd3b0c
5ba269c
File filter
Filter by extension
Conversations
Jump to
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.
Show resolved
Hide resolved
|
||
} | ||
|
||
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).value !== undefined; | ||
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. Do any other 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. updated! |
||
} | ||
|
||
/** | ||
* 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); | ||
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 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.