Skip to content

Commit 8305b94

Browse files
authored
feat(replay): Change LCP calculation (getsentry#7187)
Fixes up calculating LCP timing based on `web-vitals`. Previously we were using the `duration` field on the PerformanceEntry, but it was always 0. Decided to change the `data.duration` field to `data.value` so that we do not break the frontend when trying to display this new field.
1 parent b66559f commit 8305b94

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

packages/integration-tests/utils/replayEventTemplates.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const expectedLCPPerformanceSpan = {
9191
startTimestamp: expect.any(Number),
9292
endTimestamp: expect.any(Number),
9393
data: {
94-
duration: expect.any(Number),
94+
value: expect.any(Number),
9595
nodeId: expect.any(Number),
9696
size: expect.any(Number),
9797
},

packages/replay/src/util/createPerformanceEntries.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,29 @@ function createResourceEntry(entry: PerformanceResourceTiming) {
104104
// TODO: type definition!
105105
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
106106
function createLargestContentfulPaint(entry: PerformanceEntry & { size: number; element: Node }) {
107-
const { duration, entryType, startTime, size } = entry;
107+
const { entryType, startTime, size } = entry;
108108

109-
const start = getAbsoluteTime(startTime);
109+
let startTimeOrNavigationActivation = 0;
110+
111+
if (WINDOW.performance) {
112+
const navEntry = WINDOW.performance.getEntriesByType('navigation')[0] as PerformanceNavigationTiming & {
113+
activationStart: number;
114+
};
115+
116+
// See https://github.com/GoogleChrome/web-vitals/blob/9f11c4c6578fb4c5ee6fa4e32b9d1d756475f135/src/lib/getActivationStart.ts#L21
117+
startTimeOrNavigationActivation = (navEntry && navEntry.activationStart) || 0;
118+
}
119+
120+
const start = getAbsoluteTime(startTimeOrNavigationActivation);
121+
const value = Math.max(startTime - startTimeOrNavigationActivation, 0);
110122

111123
return {
112124
type: entryType,
113125
name: entryType,
114126
start,
115-
end: start + duration,
127+
end: start + value,
116128
data: {
117-
duration,
129+
value,
118130
size,
119131
// Not sure why this errors, Node should be correct (Argument of type 'Node' is not assignable to parameter of type 'INode')
120132
// eslint-disable-next-line @typescript-eslint/no-explicit-any

0 commit comments

Comments
 (0)