File tree 2 files changed +18
-4
lines changed
packages/tracing/src/browser
2 files changed +18
-4
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ export class MetricsInstrumentation {
21
21
private _performanceCursor : number = 0 ;
22
22
private _lcpEntry : LargestContentfulPaint | undefined ;
23
23
private _clsEntry : LayoutShift | undefined ;
24
+ private _reportLCP : ReturnType < typeof getLCP > ;
24
25
25
26
public constructor ( private _reportAllChanges : boolean = false ) {
26
27
if ( ! isNodeEnv ( ) && global && global . performance && global . document ) {
@@ -43,6 +44,12 @@ export class MetricsInstrumentation {
43
44
44
45
logger . log ( '[Tracing] Adding & adjusting spans using Performance API' ) ;
45
46
47
+ // `addPerformanceEntries` should be called on transaction.finish
48
+ // If we are finishing a transaction, we should force report the LCP.
49
+ if ( this . _reportLCP ) {
50
+ this . _reportLCP ( true ) ;
51
+ }
52
+
46
53
const timeOrigin = msToSec ( browserPerformanceTimeOrigin ) ;
47
54
48
55
let responseStartTimestamp : number | undefined ;
@@ -223,7 +230,7 @@ export class MetricsInstrumentation {
223
230
224
231
/** Starts tracking the Largest Contentful Paint on the current page. */
225
232
private _trackLCP ( ) : void {
226
- getLCP ( metric => {
233
+ this . _reportLCP = getLCP ( metric => {
227
234
const entry = metric . entries . pop ( ) ;
228
235
if ( ! entry ) {
229
236
return ;
Original file line number Diff line number Diff line change @@ -34,10 +34,13 @@ export interface LargestContentfulPaint extends PerformanceEntry {
34
34
35
35
const reportedMetricIDs : Record < string , boolean > = { } ;
36
36
37
- export const getLCP = ( onReport : ReportHandler , reportAllChanges ?: boolean ) : void => {
37
+ export const getLCP = (
38
+ onReport : ReportHandler ,
39
+ reportAllChanges ?: boolean ,
40
+ ) : ReturnType < typeof bindReporter > | undefined => {
38
41
const visibilityWatcher = getVisibilityWatcher ( ) ;
39
42
const metric = initMetric ( 'LCP' ) ;
40
- let report : ReturnType < typeof bindReporter > ;
43
+ let report : ReturnType < typeof bindReporter > | undefined ;
41
44
42
45
const entryHandler = ( entry : PerformanceEntry ) : void => {
43
46
// The startTime attribute returns the value of the renderTime if it is not 0,
@@ -66,7 +69,9 @@ export const getLCP = (onReport: ReportHandler, reportAllChanges?: boolean): voi
66
69
po . takeRecords ( ) . map ( entryHandler as PerformanceEntryHandler ) ;
67
70
po . disconnect ( ) ;
68
71
reportedMetricIDs [ metric . id ] = true ;
69
- report ( true ) ;
72
+ if ( report ) {
73
+ report ( true ) ;
74
+ }
70
75
}
71
76
} ;
72
77
@@ -79,4 +84,6 @@ export const getLCP = (onReport: ReportHandler, reportAllChanges?: boolean): voi
79
84
80
85
onHidden ( stopListening , true ) ;
81
86
}
87
+
88
+ return report ;
82
89
} ;
You can’t perform that action at this time.
0 commit comments