Skip to content

Commit b29940a

Browse files
logaretmclaude
andcommitted
feat(browser-utils): Add FCP instrumentation handler and export INP_ENTRY_MAP
Add `addFcpInstrumentationHandler` using the existing `onFCP` web-vitals library integration, following the same pattern as the other metric handlers. Export `INP_ENTRY_MAP` from inp.ts for reuse in the new web vital spans module. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 5963170 commit b29940a

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

packages/browser-utils/src/metrics/inp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function startTrackingINP(): () => void {
5454
return () => undefined;
5555
}
5656

57-
const INP_ENTRY_MAP: Record<string, 'click' | 'hover' | 'drag' | 'press'> = {
57+
export const INP_ENTRY_MAP: Record<string, 'click' | 'hover' | 'drag' | 'press'> = {
5858
click: 'click',
5959
pointerdown: 'click',
6060
pointerup: 'click',

packages/browser-utils/src/metrics/instrument.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { onCLS } from './web-vitals/getCLS';
44
import { onINP } from './web-vitals/getINP';
55
import { onLCP } from './web-vitals/getLCP';
66
import { observe } from './web-vitals/lib/observe';
7+
import { onFCP } from './web-vitals/onFCP';
78
import { onTTFB } from './web-vitals/onTTFB';
89

910
type InstrumentHandlerTypePerformanceObserver =
@@ -16,7 +17,7 @@ type InstrumentHandlerTypePerformanceObserver =
1617
// fist-input is still needed for INP
1718
| 'first-input';
1819

19-
type InstrumentHandlerTypeMetric = 'cls' | 'lcp' | 'ttfb' | 'inp';
20+
type InstrumentHandlerTypeMetric = 'cls' | 'lcp' | 'ttfb' | 'inp' | 'fcp';
2021

2122
// We provide this here manually instead of relying on a global, as this is not available in non-browser environements
2223
// And we do not want to expose such types
@@ -114,6 +115,7 @@ let _previousCls: Metric | undefined;
114115
let _previousLcp: Metric | undefined;
115116
let _previousTtfb: Metric | undefined;
116117
let _previousInp: Metric | undefined;
118+
let _previousFcp: Metric | undefined;
117119

118120
/**
119121
* Add a callback that will be triggered when a CLS metric is available.
@@ -164,6 +166,14 @@ export function addInpInstrumentationHandler(callback: InstrumentationHandlerCal
164166
return addMetricObserver('inp', callback, instrumentInp, _previousInp);
165167
}
166168

169+
/**
170+
* Add a callback that will be triggered when a FCP metric is available.
171+
* Returns a cleanup callback which can be called to remove the instrumentation handler.
172+
*/
173+
export function addFcpInstrumentationHandler(callback: (data: { metric: Metric }) => void): CleanupHandlerCallback {
174+
return addMetricObserver('fcp', callback, instrumentFcp, _previousFcp);
175+
}
176+
167177
export function addPerformanceInstrumentationHandler(
168178
type: 'event',
169179
callback: (data: { entries: ((PerformanceEntry & { target?: unknown | null }) | PerformanceEventTiming)[] }) => void,
@@ -259,6 +269,15 @@ function instrumentInp(): void {
259269
});
260270
}
261271

272+
function instrumentFcp(): StopListening {
273+
return onFCP(metric => {
274+
triggerHandlers('fcp', {
275+
metric,
276+
});
277+
_previousFcp = metric;
278+
});
279+
}
280+
262281
function addMetricObserver(
263282
type: InstrumentHandlerTypeMetric,
264283
callback: InstrumentHandlerCallback,

0 commit comments

Comments
 (0)