Skip to content

Commit

Permalink
fix(replays): Improve replay web vital types (#13602)
Browse files Browse the repository at this point in the history
Removes optional types so that type mismatches would be caught by
typescript

Follow up to #13573
  • Loading branch information
c298lee committed Sep 6, 2024
1 parent 8a8f08f commit e5d1575
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/replay-internal/src/types/performance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export interface WebVitalData {
/**
* The layout shifts of a CLS metric
*/
attributions?: { value: number; nodeIds?: number[] }[];
attributions?: { value: number; nodeIds: number[] | undefined }[];
}

/**
Expand Down
16 changes: 5 additions & 11 deletions packages/replay-internal/src/util/createPerformanceEntries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ interface LayoutShiftAttribution {
currentRect: DOMRectReadOnly;
}

interface Attribution {
value: number;
nodeIds?: number[];
}

/**
* Handler creater for web vitals
*/
Expand Down Expand Up @@ -206,7 +201,7 @@ function isLayoutShift(entry: PerformanceEntry): entry is LayoutShift {
* Add a CLS event to the replay based on a CLS metric.
*/
export function getCumulativeLayoutShift(metric: Metric): ReplayPerformanceEntry<WebVitalData> {
const layoutShifts: Attribution[] = [];
const layoutShifts: WebVitalData['attributions'] = [];
const nodes: Node[] = [];
for (const entry of metric.entries) {
if (isLayoutShift(entry)) {
Expand All @@ -220,9 +215,10 @@ export function getCumulativeLayoutShift(metric: Metric): ReplayPerformanceEntry
}
}
}
layoutShifts.push({ value: entry.value, nodeIds });
layoutShifts.push({ value: entry.value, nodeIds: nodeIds.length ? nodeIds : undefined });
}
}

return getWebVital(metric, 'cumulative-layout-shift', nodes, layoutShifts);
}

Expand Down Expand Up @@ -251,14 +247,14 @@ function getWebVital(
metric: Metric,
name: string,
nodes: Node[] | undefined,
attributions?: Attribution[],
attributions?: WebVitalData['attributions'],
): ReplayPerformanceEntry<WebVitalData> {
const value = metric.value;
const rating = metric.rating;

const end = getAbsoluteTime(value);

const data: ReplayPerformanceEntry<WebVitalData> = {
return {
type: 'web-vital',
name,
start: end,
Expand All @@ -271,6 +267,4 @@ function getWebVital(
attributions,
},
};

return data;
}

0 comments on commit e5d1575

Please sign in to comment.