1- import { record } from '@sentry-internal/rrweb' ;
1+ import type { Mirror } from '@sentry-internal/rrweb-snapshot ' ;
22import { browserPerformanceTimeOrigin } from '@sentry/utils' ;
33
44import { WINDOW } from '../constants' ;
@@ -17,7 +17,7 @@ import type {
1717// Map entryType -> function to normalize data for event
1818const ENTRY_TYPES : Record <
1919 string ,
20- ( entry : AllPerformanceEntry ) => null | ReplayPerformanceEntry < AllPerformanceEntryData >
20+ ( entry : AllPerformanceEntry , mirror : Mirror ) => null | ReplayPerformanceEntry < AllPerformanceEntryData >
2121> = {
2222 // @ts -expect-error TODO: entry type does not fit the create* functions entry type
2323 resource : createResourceEntry ,
@@ -56,27 +56,33 @@ interface LayoutShiftAttribution {
5656 * Handler creater for web vitals
5757 */
5858export function webVitalHandler (
59- getter : ( metric : Metric ) => ReplayPerformanceEntry < AllPerformanceEntryData > ,
59+ getter : ( metric : Metric , mirror : Mirror ) => ReplayPerformanceEntry < AllPerformanceEntryData > ,
6060 replay : ReplayContainer ,
6161) : ( data : { metric : Metric } ) => void {
62- return ( { metric } ) => void replay . replayPerformanceEntries . push ( getter ( metric ) ) ;
62+ return ( { metric } ) => void replay . replayPerformanceEntries . push ( getter ( metric , replay . getDomMirror ( ) ) ) ;
6363}
6464
6565/**
6666 * Create replay performance entries from the browser performance entries.
6767 */
6868export function createPerformanceEntries (
6969 entries : AllPerformanceEntry [ ] ,
70+ mirror : Mirror ,
7071) : ReplayPerformanceEntry < AllPerformanceEntryData > [ ] {
71- return entries . map ( createPerformanceEntry ) . filter ( Boolean ) as ReplayPerformanceEntry < AllPerformanceEntryData > [ ] ;
72+ return entries
73+ . map ( entry => createPerformanceEntry ( entry , mirror ) )
74+ . filter ( Boolean ) as ReplayPerformanceEntry < AllPerformanceEntryData > [ ] ;
7275}
7376
74- function createPerformanceEntry ( entry : AllPerformanceEntry ) : ReplayPerformanceEntry < AllPerformanceEntryData > | null {
77+ function createPerformanceEntry (
78+ entry : AllPerformanceEntry ,
79+ mirror : Mirror ,
80+ ) : ReplayPerformanceEntry < AllPerformanceEntryData > | null {
7581 if ( ! ENTRY_TYPES [ entry . entryType ] ) {
7682 return null ;
7783 }
7884
79- return ENTRY_TYPES [ entry . entryType ] ( entry ) ;
85+ return ENTRY_TYPES [ entry . entryType ] ( entry , mirror ) ;
8086}
8187
8288function getAbsoluteTime ( time : number ) : number {
@@ -85,7 +91,7 @@ function getAbsoluteTime(time: number): number {
8591 return ( ( browserPerformanceTimeOrigin || WINDOW . performance . timeOrigin ) + time ) / 1000 ;
8692}
8793
88- function createPaintEntry ( entry : PerformancePaintTiming ) : ReplayPerformanceEntry < PaintData > {
94+ function createPaintEntry ( entry : PerformancePaintTiming , _mirror : Mirror ) : ReplayPerformanceEntry < PaintData > {
8995 const { duration, entryType, name, startTime } = entry ;
9096
9197 const start = getAbsoluteTime ( startTime ) ;
@@ -98,7 +104,10 @@ function createPaintEntry(entry: PerformancePaintTiming): ReplayPerformanceEntry
98104 } ;
99105}
100106
101- function createNavigationEntry ( entry : PerformanceNavigationTiming ) : ReplayPerformanceEntry < NavigationData > | null {
107+ function createNavigationEntry (
108+ entry : PerformanceNavigationTiming ,
109+ _mirror : Mirror ,
110+ ) : ReplayPerformanceEntry < NavigationData > | null {
102111 const {
103112 entryType,
104113 name,
@@ -145,6 +154,7 @@ function createNavigationEntry(entry: PerformanceNavigationTiming): ReplayPerfor
145154
146155function createResourceEntry (
147156 entry : ExperimentalPerformanceResourceTiming ,
157+ _mirror : Mirror ,
148158) : ReplayPerformanceEntry < ResourceData > | null {
149159 const {
150160 entryType,
@@ -180,38 +190,38 @@ function createResourceEntry(
180190/**
181191 * Add a LCP event to the replay based on a LCP metric.
182192 */
183- export function getLargestContentfulPaint ( metric : Metric ) : ReplayPerformanceEntry < WebVitalData > {
193+ export function getLargestContentfulPaint ( metric : Metric , mirror : Mirror ) : ReplayPerformanceEntry < WebVitalData > {
184194 const lastEntry = metric . entries [ metric . entries . length - 1 ] as ( PerformanceEntry & { element ?: Node } ) | undefined ;
185195 const node = lastEntry ? lastEntry . element : undefined ;
186- return getWebVital ( metric , 'largest-contentful-paint' , node ) ;
196+ return getWebVital ( metric , 'largest-contentful-paint' , node , mirror ) ;
187197}
188198
189199/**
190200 * Add a CLS event to the replay based on a CLS metric.
191201 */
192- export function getCumulativeLayoutShift ( metric : Metric ) : ReplayPerformanceEntry < WebVitalData > {
202+ export function getCumulativeLayoutShift ( metric : Metric , mirror : Mirror ) : ReplayPerformanceEntry < WebVitalData > {
193203 // get first node that shifts
194204 const firstEntry = metric . entries [ 0 ] as ( PerformanceEntry & { sources ?: LayoutShiftAttribution [ ] } ) | undefined ;
195205 const node = firstEntry ? ( firstEntry . sources ? firstEntry . sources [ 0 ] . node : undefined ) : undefined ;
196- return getWebVital ( metric , 'cumulative-layout-shift' , node ) ;
206+ return getWebVital ( metric , 'cumulative-layout-shift' , node , mirror ) ;
197207}
198208
199209/**
200210 * Add a FID event to the replay based on a FID metric.
201211 */
202- export function getFirstInputDelay ( metric : Metric ) : ReplayPerformanceEntry < WebVitalData > {
212+ export function getFirstInputDelay ( metric : Metric , mirror : Mirror ) : ReplayPerformanceEntry < WebVitalData > {
203213 const lastEntry = metric . entries [ metric . entries . length - 1 ] as ( PerformanceEntry & { target ?: Node } ) | undefined ;
204214 const node = lastEntry ? lastEntry . target : undefined ;
205- return getWebVital ( metric , 'first-input-delay' , node ) ;
215+ return getWebVital ( metric , 'first-input-delay' , node , mirror ) ;
206216}
207217
208218/**
209219 * Add an INP event to the replay based on an INP metric.
210220 */
211- export function getInteractionToNextPaint ( metric : Metric ) : ReplayPerformanceEntry < WebVitalData > {
221+ export function getInteractionToNextPaint ( metric : Metric , mirror : Mirror ) : ReplayPerformanceEntry < WebVitalData > {
212222 const lastEntry = metric . entries [ metric . entries . length - 1 ] as ( PerformanceEntry & { target ?: Node } ) | undefined ;
213223 const node = lastEntry ? lastEntry . target : undefined ;
214- return getWebVital ( metric , 'interaction-to-next-paint' , node ) ;
224+ return getWebVital ( metric , 'interaction-to-next-paint' , node , mirror ) ;
215225}
216226
217227/**
@@ -221,6 +231,7 @@ export function getWebVital(
221231 metric : Metric ,
222232 name : string ,
223233 node : Node | undefined ,
234+ mirror : Mirror | undefined ,
224235) : ReplayPerformanceEntry < WebVitalData > {
225236 const value = metric . value ;
226237 const rating = metric . rating ;
@@ -236,7 +247,7 @@ export function getWebVital(
236247 value,
237248 size : value ,
238249 rating,
239- nodeId : node ? record . mirror . getId ( node ) : undefined ,
250+ nodeId : node && mirror ? mirror . getId ( node ) : undefined ,
240251 } ,
241252 } ;
242253
0 commit comments