Skip to content

Commit 87b789c

Browse files
authored
feat(browser-utils): Update web-vitals to v4.2.4 (#14439)
Update the vendored `web-vitals` library from [3.5.2 to 4.2.4](https://github.com/GoogleChrome/web-vitals/blob/main/CHANGELOG.md) Some noteable `web-vitals` changes: - breaking type changes (version 4.0.0) - INP fixes and code refactors - minor LCP fix for web vitals library being late-initialized. I don't think this applies to us but who knows... Further changes from our end: - The `onHidden` utility function was NOT updated to 4.2.4 due to the new version no longer triggering correctly for Safari 12.1-14.0 (which we [still support](https://docs.sentry.io/platforms/javascript/troubleshooting/supported-browsers/)). More details in the code comment - Added an optional param to `getNavigationEntry` since ww 4.2.4 only returns the entry if the `responseStart` time value is plausible. This is a good change for the library but since we also use the function to create other spans and attributes, I opted to leave things as they are for these use cases by passing in the flag to skip the plausibility check. This seems to be primarily a problem with Safari, which reports `responseStart: 0` sometimes. - Continued to add checks for the existence of `WINDOW.document` which `web-vitals` assumes to be present - Continued to add `longtask` to the array of available types in the `observe` function
1 parent 9b9ec77 commit 87b789c

26 files changed

+443
-375
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ export function _addMeasureSpans(
420420
duration: number,
421421
timeOrigin: number,
422422
): number {
423-
const navEntry = getNavigationEntry();
423+
const navEntry = getNavigationEntry(false);
424424
const requestTime = msToSec(navEntry ? navEntry.requestStart : 0);
425425
// Because performance.measure accepts arbitrary timestamps it can produce
426426
// spans that happen before the browser even makes a request for the page.
@@ -671,7 +671,7 @@ function setResourceEntrySizeData(
671671
* ttfb information is added via vendored web vitals library.
672672
*/
673673
function _addTtfbRequestTimeToMeasurements(_measurements: Measurements): void {
674-
const navEntry = getNavigationEntry();
674+
const navEntry = getNavigationEntry(false);
675675
if (!navEntry) {
676676
return;
677677
}

packages/browser-utils/src/metrics/web-vitals/README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
This was vendored from: https://github.com/GoogleChrome/web-vitals: v3.5.2
66

77
The commit SHA used is:
8-
[7b44bea0d5ba6629c5fd34c3a09cc683077871d0](https://github.com/GoogleChrome/web-vitals/tree/7b44bea0d5ba6629c5fd34c3a09cc683077871d0)
8+
[3d2b3dc8576cc003618952fa39902fab764a53e2](https://github.com/GoogleChrome/web-vitals/tree/3d2b3dc8576cc003618952fa39902fab764a53e2)
99

1010
Current vendored web vitals are:
1111

@@ -27,6 +27,14 @@ web-vitals only report once per pageload.
2727

2828
## CHANGELOG
2929

30+
https://github.com/getsentry/sentry-javascript/pull/14439
31+
32+
- Bumped from Web Vitals v3.5.2 to v4.2.4
33+
34+
https://github.com/getsentry/sentry-javascript/pull/11391
35+
36+
- Bumped from Web Vitals v3.0.4 to v3.5.2
37+
3038
https://github.com/getsentry/sentry-javascript/pull/5987
3139

3240
- Bumped from Web Vitals v2.1.0 to v3.0.4

packages/browser-utils/src/metrics/web-vitals/getCLS.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { observe } from './lib/observe';
2020
import { onHidden } from './lib/onHidden';
2121
import { runOnce } from './lib/runOnce';
2222
import { onFCP } from './onFCP';
23-
import type { CLSMetric, CLSReportCallback, MetricRatingThresholds, ReportOpts } from './types';
23+
import type { CLSMetric, MetricRatingThresholds, ReportOpts } from './types';
2424

2525
/** Thresholds for CLS. See https://web.dev/articles/cls#what_is_a_good_cls_score */
2626
export const CLSThresholds: MetricRatingThresholds = [0.1, 0.25];
@@ -46,7 +46,7 @@ export const CLSThresholds: MetricRatingThresholds = [0.1, 0.25];
4646
* hidden. As a result, the `callback` function might be called multiple times
4747
* during the same page load._
4848
*/
49-
export const onCLS = (onReport: CLSReportCallback, opts: ReportOpts = {}): void => {
49+
export const onCLS = (onReport: (metric: CLSMetric) => void, opts: ReportOpts = {}) => {
5050
// Start monitoring FCP so we can only report CLS if FCP is also reported.
5151
// Note: this is done to match the current behavior of CrUX.
5252
onFCP(
@@ -57,7 +57,7 @@ export const onCLS = (onReport: CLSReportCallback, opts: ReportOpts = {}): void
5757
let sessionValue = 0;
5858
let sessionEntries: LayoutShift[] = [];
5959

60-
const handleEntries = (entries: LayoutShift[]): void => {
60+
const handleEntries = (entries: LayoutShift[]) => {
6161
entries.forEach(entry => {
6262
// Only count layout shifts without recent user input.
6363
if (!entry.hadRecentInput) {

packages/browser-utils/src/metrics/web-vitals/getFID.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { observe } from './lib/observe';
2121
import { onHidden } from './lib/onHidden';
2222
import { runOnce } from './lib/runOnce';
2323
import { whenActivated } from './lib/whenActivated';
24-
import type { FIDMetric, FIDReportCallback, MetricRatingThresholds, ReportOpts } from './types';
24+
import type { FIDMetric, MetricRatingThresholds, ReportOpts } from './types';
2525

2626
/** Thresholds for FID. See https://web.dev/articles/fid#what_is_a_good_fid_score */
2727
export const FIDThresholds: MetricRatingThresholds = [100, 300];
@@ -35,7 +35,7 @@ export const FIDThresholds: MetricRatingThresholds = [100, 300];
3535
* _**Important:** since FID is only reported after the user interacts with the
3636
* page, it's possible that it will not be reported for some page loads._
3737
*/
38-
export const onFID = (onReport: FIDReportCallback, opts: ReportOpts = {}) => {
38+
export const onFID = (onReport: (metric: FIDMetric) => void, opts: ReportOpts = {}) => {
3939
whenActivated(() => {
4040
const visibilityWatcher = getVisibilityWatcher();
4141
const metric = initMetric('FID');
@@ -56,6 +56,7 @@ export const onFID = (onReport: FIDReportCallback, opts: ReportOpts = {}) => {
5656
};
5757

5858
const po = observe('first-input', handleEntries);
59+
5960
report = bindReporter(onReport, metric, FIDThresholds, opts.reportAllChanges);
6061

6162
if (po) {

packages/browser-utils/src/metrics/web-vitals/getINP.ts

+29-131
Original file line numberDiff line numberDiff line change
@@ -17,100 +17,18 @@
1717
import { WINDOW } from '../../types';
1818
import { bindReporter } from './lib/bindReporter';
1919
import { initMetric } from './lib/initMetric';
20+
import { DEFAULT_DURATION_THRESHOLD, estimateP98LongestInteraction, processInteractionEntry } from './lib/interactions';
2021
import { observe } from './lib/observe';
2122
import { onHidden } from './lib/onHidden';
22-
import { getInteractionCount, initInteractionCountPolyfill } from './lib/polyfills/interactionCountPolyfill';
23+
import { initInteractionCountPolyfill } from './lib/polyfills/interactionCountPolyfill';
2324
import { whenActivated } from './lib/whenActivated';
24-
import type { INPMetric, INPReportCallback, MetricRatingThresholds, ReportOpts } from './types';
25+
import { whenIdle } from './lib/whenIdle';
2526

26-
interface Interaction {
27-
id: number;
28-
latency: number;
29-
entries: PerformanceEventTiming[];
30-
}
27+
import type { INPMetric, MetricRatingThresholds, ReportOpts } from './types';
3128

3229
/** Thresholds for INP. See https://web.dev/articles/inp#what_is_a_good_inp_score */
3330
export const INPThresholds: MetricRatingThresholds = [200, 500];
3431

35-
// Used to store the interaction count after a bfcache restore, since p98
36-
// interaction latencies should only consider the current navigation.
37-
const prevInteractionCount = 0;
38-
39-
/**
40-
* Returns the interaction count since the last bfcache restore (or for the
41-
* full page lifecycle if there were no bfcache restores).
42-
*/
43-
const getInteractionCountForNavigation = () => {
44-
return getInteractionCount() - prevInteractionCount;
45-
};
46-
47-
// To prevent unnecessary memory usage on pages with lots of interactions,
48-
// store at most 10 of the longest interactions to consider as INP candidates.
49-
const MAX_INTERACTIONS_TO_CONSIDER = 10;
50-
51-
// A list of longest interactions on the page (by latency) sorted so the
52-
// longest one is first. The list is as most MAX_INTERACTIONS_TO_CONSIDER long.
53-
const longestInteractionList: Interaction[] = [];
54-
55-
// A mapping of longest interactions by their interaction ID.
56-
// This is used for faster lookup.
57-
const longestInteractionMap: { [interactionId: string]: Interaction } = {};
58-
59-
/**
60-
* Takes a performance entry and adds it to the list of worst interactions
61-
* if its duration is long enough to make it among the worst. If the
62-
* entry is part of an existing interaction, it is merged and the latency
63-
* and entries list is updated as needed.
64-
*/
65-
const processEntry = (entry: PerformanceEventTiming) => {
66-
// The least-long of the 10 longest interactions.
67-
const minLongestInteraction = longestInteractionList[longestInteractionList.length - 1];
68-
69-
const existingInteraction = longestInteractionMap[entry.interactionId!];
70-
71-
// Only process the entry if it's possibly one of the ten longest,
72-
// or if it's part of an existing interaction.
73-
if (
74-
existingInteraction ||
75-
longestInteractionList.length < MAX_INTERACTIONS_TO_CONSIDER ||
76-
(minLongestInteraction && entry.duration > minLongestInteraction.latency)
77-
) {
78-
// If the interaction already exists, update it. Otherwise create one.
79-
if (existingInteraction) {
80-
existingInteraction.entries.push(entry);
81-
existingInteraction.latency = Math.max(existingInteraction.latency, entry.duration);
82-
} else {
83-
const interaction = {
84-
id: entry.interactionId!,
85-
latency: entry.duration,
86-
entries: [entry],
87-
};
88-
longestInteractionMap[interaction.id] = interaction;
89-
longestInteractionList.push(interaction);
90-
}
91-
92-
// Sort the entries by latency (descending) and keep only the top ten.
93-
longestInteractionList.sort((a, b) => b.latency - a.latency);
94-
longestInteractionList.splice(MAX_INTERACTIONS_TO_CONSIDER).forEach(i => {
95-
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
96-
delete longestInteractionMap[i.id];
97-
});
98-
}
99-
};
100-
101-
/**
102-
* Returns the estimated p98 longest interaction based on the stored
103-
* interaction candidates and the interaction count for the current page.
104-
*/
105-
const estimateP98LongestInteraction = () => {
106-
const candidateInteractionIndex = Math.min(
107-
longestInteractionList.length - 1,
108-
Math.floor(getInteractionCountForNavigation() / 50),
109-
);
110-
111-
return longestInteractionList[candidateInteractionIndex];
112-
};
113-
11432
/**
11533
* Calculates the [INP](https://web.dev/articles/inp) value for the current
11634
* page and calls the `callback` function once the value is ready, along with
@@ -138,7 +56,12 @@ const estimateP98LongestInteraction = () => {
13856
* hidden. As a result, the `callback` function might be called multiple times
13957
* during the same page load._
14058
*/
141-
export const onINP = (onReport: INPReportCallback, opts: ReportOpts = {}) => {
59+
export const onINP = (onReport: (metric: INPMetric) => void, opts: ReportOpts = {}) => {
60+
// Return if the browser doesn't support all APIs needed to measure INP.
61+
if (!('PerformanceEventTiming' in WINDOW && 'interactionId' in PerformanceEventTiming.prototype)) {
62+
return;
63+
}
64+
14265
whenActivated(() => {
14366
// TODO(philipwalton): remove once the polyfill is no longer needed.
14467
initInteractionCountPolyfill();
@@ -148,37 +71,23 @@ export const onINP = (onReport: INPReportCallback, opts: ReportOpts = {}) => {
14871
let report: ReturnType<typeof bindReporter>;
14972

15073
const handleEntries = (entries: INPMetric['entries']) => {
151-
entries.forEach(entry => {
152-
if (entry.interactionId) {
153-
processEntry(entry);
154-
}
155-
156-
// Entries of type `first-input` don't currently have an `interactionId`,
157-
// so to consider them in INP we have to first check that an existing
158-
// entry doesn't match the `duration` and `startTime`.
159-
// Note that this logic assumes that `event` entries are dispatched
160-
// before `first-input` entries. This is true in Chrome (the only browser
161-
// that currently supports INP).
162-
// TODO(philipwalton): remove once crbug.com/1325826 is fixed.
163-
if (entry.entryType === 'first-input') {
164-
const noMatchingEntry = !longestInteractionList.some(interaction => {
165-
return interaction.entries.some(prevEntry => {
166-
return entry.duration === prevEntry.duration && entry.startTime === prevEntry.startTime;
167-
});
168-
});
169-
if (noMatchingEntry) {
170-
processEntry(entry);
171-
}
74+
// Queue the `handleEntries()` callback in the next idle task.
75+
// This is needed to increase the chances that all event entries that
76+
// occurred between the user interaction and the next paint
77+
// have been dispatched. Note: there is currently an experiment
78+
// running in Chrome (EventTimingKeypressAndCompositionInteractionId)
79+
// 123+ that if rolled out fully may make this no longer necessary.
80+
whenIdle(() => {
81+
entries.forEach(processInteractionEntry);
82+
83+
const inp = estimateP98LongestInteraction();
84+
85+
if (inp && inp.latency !== metric.value) {
86+
metric.value = inp.latency;
87+
metric.entries = inp.entries;
88+
report();
17289
}
17390
});
174-
175-
const inp = estimateP98LongestInteraction();
176-
177-
if (inp && inp.latency !== metric.value) {
178-
metric.value = inp.latency;
179-
metric.entries = inp.entries;
180-
report();
181-
}
18291
};
18392

18493
const po = observe('event', handleEntries, {
@@ -188,29 +97,18 @@ export const onINP = (onReport: INPReportCallback, opts: ReportOpts = {}) => {
18897
// and performance. Running this callback for any interaction that spans
18998
// just one or two frames is likely not worth the insight that could be
19099
// gained.
191-
durationThreshold: opts.durationThreshold != null ? opts.durationThreshold : 40,
192-
} as PerformanceObserverInit);
100+
durationThreshold: opts.durationThreshold != null ? opts.durationThreshold : DEFAULT_DURATION_THRESHOLD,
101+
});
193102

194103
report = bindReporter(onReport, metric, INPThresholds, opts.reportAllChanges);
195104

196105
if (po) {
197-
// If browser supports interactionId (and so supports INP), also
198-
// observe entries of type `first-input`. This is useful in cases
106+
// Also observe entries of type `first-input`. This is useful in cases
199107
// where the first interaction is less than the `durationThreshold`.
200-
if ('PerformanceEventTiming' in WINDOW && 'interactionId' in PerformanceEventTiming.prototype) {
201-
po.observe({ type: 'first-input', buffered: true });
202-
}
108+
po.observe({ type: 'first-input', buffered: true });
203109

204110
onHidden(() => {
205111
handleEntries(po.takeRecords() as INPMetric['entries']);
206-
207-
// If the interaction count shows that there were interactions but
208-
// none were captured by the PerformanceObserver, report a latency of 0.
209-
if (metric.value < 0 && getInteractionCountForNavigation() > 0) {
210-
metric.value = 0;
211-
metric.entries = [];
212-
}
213-
214112
report(true);
215113
});
216114
}

packages/browser-utils/src/metrics/web-vitals/getLCP.ts

+23-13
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import { observe } from './lib/observe';
2323
import { onHidden } from './lib/onHidden';
2424
import { runOnce } from './lib/runOnce';
2525
import { whenActivated } from './lib/whenActivated';
26-
import type { LCPMetric, LCPReportCallback, MetricRatingThresholds, ReportOpts } from './types';
26+
import { whenIdle } from './lib/whenIdle';
27+
import type { LCPMetric, MetricRatingThresholds, ReportOpts } from './types';
2728

2829
/** Thresholds for LCP. See https://web.dev/articles/lcp#what_is_a_good_lcp_score */
2930
export const LCPThresholds: MetricRatingThresholds = [2500, 4000];
@@ -41,28 +42,34 @@ const reportedMetricIDs: Record<string, boolean> = {};
4142
* performance entry is dispatched, or once the final value of the metric has
4243
* been determined.
4344
*/
44-
export const onLCP = (onReport: LCPReportCallback, opts: ReportOpts = {}) => {
45+
export const onLCP = (onReport: (metric: LCPMetric) => void, opts: ReportOpts = {}) => {
4546
whenActivated(() => {
4647
const visibilityWatcher = getVisibilityWatcher();
4748
const metric = initMetric('LCP');
4849
let report: ReturnType<typeof bindReporter>;
4950

5051
const handleEntries = (entries: LCPMetric['entries']) => {
51-
const lastEntry = entries[entries.length - 1] as LargestContentfulPaint;
52-
if (lastEntry) {
52+
// If reportAllChanges is set then call this function for each entry,
53+
// otherwise only consider the last one.
54+
if (!opts.reportAllChanges) {
55+
// eslint-disable-next-line no-param-reassign
56+
entries = entries.slice(-1);
57+
}
58+
59+
entries.forEach(entry => {
5360
// Only report if the page wasn't hidden prior to LCP.
54-
if (lastEntry.startTime < visibilityWatcher.firstHiddenTime) {
61+
if (entry.startTime < visibilityWatcher.firstHiddenTime) {
5562
// The startTime attribute returns the value of the renderTime if it is
5663
// not 0, and the value of the loadTime otherwise. The activationStart
5764
// reference is used because LCP should be relative to page activation
58-
// rather than navigation start if the page was prerendered. But in cases
65+
// rather than navigation start if the page was pre-rendered. But in cases
5966
// where `activationStart` occurs after the LCP, this time should be
6067
// clamped at 0.
61-
metric.value = Math.max(lastEntry.startTime - getActivationStart(), 0);
62-
metric.entries = [lastEntry];
68+
metric.value = Math.max(entry.startTime - getActivationStart(), 0);
69+
metric.entries = [entry];
6370
report();
6471
}
65-
}
72+
});
6673
};
6774

6875
const po = observe('largest-contentful-paint', handleEntries);
@@ -83,11 +90,14 @@ export const onLCP = (onReport: LCPReportCallback, opts: ReportOpts = {}) => {
8390
// stops LCP observation, it's unreliable since it can be programmatically
8491
// generated. See: https://github.com/GoogleChrome/web-vitals/issues/75
8592
['keydown', 'click'].forEach(type => {
93+
// Wrap in a setTimeout so the callback is run in a separate task
94+
// to avoid extending the keyboard/click handler to reduce INP impact
95+
// https://github.com/GoogleChrome/web-vitals/issues/383
8696
if (WINDOW.document) {
87-
// Wrap in a setTimeout so the callback is run in a separate task
88-
// to avoid extending the keyboard/click handler to reduce INP impact
89-
// https://github.com/GoogleChrome/web-vitals/issues/383
90-
addEventListener(type, () => setTimeout(stopListening, 0), true);
97+
addEventListener(type, () => whenIdle(stopListening as () => void), {
98+
once: true,
99+
capture: true,
100+
});
91101
}
92102
});
93103

packages/browser-utils/src/metrics/web-vitals/lib/generateUniqueID.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
* @return {string}
2121
*/
2222
export const generateUniqueID = () => {
23-
return `v3-${Date.now()}-${Math.floor(Math.random() * (9e12 - 1)) + 1e12}`;
23+
return `v4-${Date.now()}-${Math.floor(Math.random() * (9e12 - 1)) + 1e12}`;
2424
};

0 commit comments

Comments
 (0)