Skip to content

Commit e68a672

Browse files
committed
force report lcp on finish
1 parent e0f9ca0 commit e68a672

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

packages/tracing/src/browser/metrics.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export class MetricsInstrumentation {
2121
private _performanceCursor: number = 0;
2222
private _lcpEntry: LargestContentfulPaint | undefined;
2323
private _clsEntry: LayoutShift | undefined;
24+
private _reportLCP: ReturnType<typeof getLCP>;
2425

2526
public constructor(private _reportAllChanges: boolean = false) {
2627
if (!isNodeEnv() && global && global.performance && global.document) {
@@ -43,6 +44,12 @@ export class MetricsInstrumentation {
4344

4445
logger.log('[Tracing] Adding & adjusting spans using Performance API');
4546

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+
4653
const timeOrigin = msToSec(browserPerformanceTimeOrigin);
4754

4855
let responseStartTimestamp: number | undefined;
@@ -223,7 +230,7 @@ export class MetricsInstrumentation {
223230

224231
/** Starts tracking the Largest Contentful Paint on the current page. */
225232
private _trackLCP(): void {
226-
getLCP(metric => {
233+
this._reportLCP = getLCP(metric => {
227234
const entry = metric.entries.pop();
228235
if (!entry) {
229236
return;

packages/tracing/src/browser/web-vitals/getLCP.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ export interface LargestContentfulPaint extends PerformanceEntry {
3434

3535
const reportedMetricIDs: Record<string, boolean> = {};
3636

37-
export const getLCP = (onReport: ReportHandler, reportAllChanges?: boolean): void => {
37+
export const getLCP = (
38+
onReport: ReportHandler,
39+
reportAllChanges?: boolean,
40+
): ReturnType<typeof bindReporter> | undefined => {
3841
const visibilityWatcher = getVisibilityWatcher();
3942
const metric = initMetric('LCP');
40-
let report: ReturnType<typeof bindReporter>;
43+
let report: ReturnType<typeof bindReporter> | undefined;
4144

4245
const entryHandler = (entry: PerformanceEntry): void => {
4346
// 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
6669
po.takeRecords().map(entryHandler as PerformanceEntryHandler);
6770
po.disconnect();
6871
reportedMetricIDs[metric.id] = true;
69-
report(true);
72+
if (report) {
73+
report(true);
74+
}
7075
}
7176
};
7277

@@ -79,4 +84,6 @@ export const getLCP = (onReport: ReportHandler, reportAllChanges?: boolean): voi
7984

8085
onHidden(stopListening, true);
8186
}
87+
88+
return report;
8289
};

0 commit comments

Comments
 (0)