Skip to content

Commit e5d1575

Browse files
authored
fix(replays): Improve replay web vital types (#13602)
Removes optional types so that type mismatches would be caught by typescript Follow up to #13573
1 parent 8a8f08f commit e5d1575

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

packages/replay-internal/src/types/performance.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export interface WebVitalData {
114114
/**
115115
* The layout shifts of a CLS metric
116116
*/
117-
attributions?: { value: number; nodeIds?: number[] }[];
117+
attributions?: { value: number; nodeIds: number[] | undefined }[];
118118
}
119119

120120
/**

packages/replay-internal/src/util/createPerformanceEntries.ts

+5-11
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ interface LayoutShiftAttribution {
5858
currentRect: DOMRectReadOnly;
5959
}
6060

61-
interface Attribution {
62-
value: number;
63-
nodeIds?: number[];
64-
}
65-
6661
/**
6762
* Handler creater for web vitals
6863
*/
@@ -206,7 +201,7 @@ function isLayoutShift(entry: PerformanceEntry): entry is LayoutShift {
206201
* Add a CLS event to the replay based on a CLS metric.
207202
*/
208203
export function getCumulativeLayoutShift(metric: Metric): ReplayPerformanceEntry<WebVitalData> {
209-
const layoutShifts: Attribution[] = [];
204+
const layoutShifts: WebVitalData['attributions'] = [];
210205
const nodes: Node[] = [];
211206
for (const entry of metric.entries) {
212207
if (isLayoutShift(entry)) {
@@ -220,9 +215,10 @@ export function getCumulativeLayoutShift(metric: Metric): ReplayPerformanceEntry
220215
}
221216
}
222217
}
223-
layoutShifts.push({ value: entry.value, nodeIds });
218+
layoutShifts.push({ value: entry.value, nodeIds: nodeIds.length ? nodeIds : undefined });
224219
}
225220
}
221+
226222
return getWebVital(metric, 'cumulative-layout-shift', nodes, layoutShifts);
227223
}
228224

@@ -251,14 +247,14 @@ function getWebVital(
251247
metric: Metric,
252248
name: string,
253249
nodes: Node[] | undefined,
254-
attributions?: Attribution[],
250+
attributions?: WebVitalData['attributions'],
255251
): ReplayPerformanceEntry<WebVitalData> {
256252
const value = metric.value;
257253
const rating = metric.rating;
258254

259255
const end = getAbsoluteTime(value);
260256

261-
const data: ReplayPerformanceEntry<WebVitalData> = {
257+
return {
262258
type: 'web-vital',
263259
name,
264260
start: end,
@@ -271,6 +267,4 @@ function getWebVital(
271267
attributions,
272268
},
273269
};
274-
275-
return data;
276270
}

0 commit comments

Comments
 (0)