Skip to content

Commit 0776cc9

Browse files
jethrongreg-el
authored andcommitted
Bump web-vitals version (#1412)
From ~3 to ~4. Also make the metric interfaces optional in preparation for v5, which will remove the (deprecated in v4) `onFID` method. The FID metric is already optional in the [schema](https://iglucentral.com/?q=web_vitals), so there should be no breaking change here when this metric becomes unavailable. We assume if the user is using the library themselves they would be manually loading it and telling us not to via: - `loadVitalsScript: false` - `webVitalsSource` override So we don't consider this a breaking change unless a user is relying on us to load v3 and with this update we start loading v4 (which has some [breaking changes](https://github.com/GoogleChrome/web-vitals/blob/main/docs/upgrading-to-v4.md)). Users in this situation can set `webVitalsSource` to continue loading v3. When v5 is released, users can opt-in to using it by specifying a v5 source for `webVitalsSource`, which should work as expected with these changes until we update the plugin's default source to be v5. Closes #1411
1 parent 5f01e36 commit 0776cc9

File tree

8 files changed

+44
-28
lines changed

8 files changed

+44
-28
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@snowplow/browser-plugin-web-vitals",
5+
"comment": "Update default external library version to v4. Add compatibility for future v5, which deprecates the FID metric.",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@snowplow/browser-plugin-web-vitals"
10+
}

common/config/rush/pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/config/rush/repo-state.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush.
22
{
3-
"pnpmShrinkwrapHash": "6f4a24fea0d73ed30b10703fd10b02f67710f7ba",
3+
"pnpmShrinkwrapHash": "483ab7c144cc1201cfba40405c05ebf190586e79",
44
"preferredVersionsHash": "bf21a9e8fbc5a3846fb05b4fa0859e0917b2202f"
55
}

plugins/browser-plugin-web-vitals/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"@snowplow/browser-tracker-core": "workspace:*",
2626
"@snowplow/tracker-core": "workspace:*",
2727
"tslib": "^2.3.1",
28-
"web-vitals": "~3.3.2"
28+
"web-vitals": "~4.2.4"
2929
},
3030
"devDependencies": {
3131
"@ampproject/rollup-plugin-closure-compiler": "~0.27.0",

plugins/browser-plugin-web-vitals/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { WEB_VITALS_SCHEMA } from './schemata';
44
import { attachWebVitalsPageListeners, createWebVitalsScript, webVitalsListener } from './utils';
55

66
const _trackers: Record<string, BrowserTracker> = {};
7-
const WEB_VITALS_SOURCE = 'https://unpkg.com/web-vitals@3/dist/web-vitals.iife.js';
7+
const WEB_VITALS_SOURCE = 'https://unpkg.com/web-vitals@4/dist/web-vitals.iife.js';
88
let listenersAttached = false;
99

1010
interface WebVitalsPluginOptions {
Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import type { ReportCallback, Metric, WebVitalsGlobal, onCLS, onLCP, onFID, onFCP, onINP, onTTFB } from 'web-vitals';
1+
import type { MetricType, Metric, onCLS, onLCP, onFID, onFCP, onINP, onTTFB } from 'web-vitals';
22

3-
export interface WebVitals extends WebVitalsGlobal {
4-
onCLS: typeof onCLS;
5-
onFID: typeof onFID;
6-
onLCP: typeof onLCP;
7-
onFCP: typeof onFCP;
8-
onINP: typeof onINP;
9-
onTTFB: typeof onTTFB;
3+
export interface WebVitals {
4+
onCLS?: typeof onCLS;
5+
onFID?: typeof onFID;
6+
onLCP?: typeof onLCP;
7+
onFCP?: typeof onFCP;
8+
onINP?: typeof onINP;
9+
onTTFB?: typeof onTTFB;
1010
}
1111

12-
export { Metric, ReportCallback };
12+
declare global {
13+
interface Window {
14+
webVitals?: WebVitals;
15+
}
16+
}
17+
18+
export { Metric, MetricType };

plugins/browser-plugin-web-vitals/src/utils.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LOG } from '@snowplow/tracker-core';
2-
import { ReportCallback, WebVitals } from './types';
2+
import { MetricType } from './types';
33

44
/**
55
* Attach page listeners to collect the Web Vitals values
@@ -51,22 +51,23 @@ export function createWebVitalsScript(webVitalsSource: string) {
5151
* @return {void}
5252
*/
5353
export function webVitalsListener(webVitalsObject: Record<string, unknown>) {
54-
function addWebVitalsMeasurement(metricSchemaName: string): ReportCallback {
54+
function addWebVitalsMeasurement(metricSchemaName: string): (arg: MetricType) => void {
5555
return (arg) => {
5656
webVitalsObject[metricSchemaName] = arg.value;
5757
webVitalsObject.navigationType = arg.navigationType;
5858
};
5959
}
60-
if (!window.webVitals) {
60+
61+
const webVitals = window.webVitals;
62+
if (!webVitals) {
6163
LOG.warn('The window.webVitals API is currently unavailable. web_vitals events will not be collected.');
6264
return;
6365
}
6466

65-
const webVitals = window.webVitals as WebVitals;
66-
webVitals.onCLS(addWebVitalsMeasurement('cls'));
67-
webVitals.onFID(addWebVitalsMeasurement('fid'));
68-
webVitals.onLCP(addWebVitalsMeasurement('lcp'));
69-
webVitals.onFCP(addWebVitalsMeasurement('fcp'));
70-
webVitals.onINP(addWebVitalsMeasurement('inp'));
71-
webVitals.onTTFB(addWebVitalsMeasurement('ttfb'));
67+
webVitals.onCLS?.(addWebVitalsMeasurement('cls'));
68+
webVitals.onFID?.(addWebVitalsMeasurement('fid'));
69+
webVitals.onLCP?.(addWebVitalsMeasurement('lcp'));
70+
webVitals.onFCP?.(addWebVitalsMeasurement('fcp'));
71+
webVitals.onINP?.(addWebVitalsMeasurement('inp'));
72+
webVitals.onTTFB?.(addWebVitalsMeasurement('ttfb'));
7273
}

plugins/browser-plugin-web-vitals/test/web-vitals.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { BrowserTracker } from '@snowplow/browser-tracker-core';
55

66
declare var jsdom: JSDOM;
77

8-
// @ts-expect-error
98
jsdom.window.webVitals = {};
109

1110
describe('Web Vitals plugin', () => {

0 commit comments

Comments
 (0)