Skip to content

Commit 9593a19

Browse files
committed
feat: remove document prefix to match web-vitals 5.1.0
1 parent bb2b7c1 commit 9593a19

File tree

5 files changed

+42
-25
lines changed

5 files changed

+42
-25
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { WINDOW } from '../../types';
1817
import { bindReporter } from './lib/bindReporter';
1918
import { getActivationStart } from './lib/getActivationStart';
2019
import { getVisibilityWatcher } from './lib/getVisibilityWatcher';
20+
import { addPageListener, removePageListener } from './lib/globalListeners';
2121
import { initMetric } from './lib/initMetric';
2222
import { initUnique } from './lib/initUnique';
2323
import { LCPEntryManager } from './lib/LCPEntryManager';
@@ -96,11 +96,9 @@ export const onLCP = (onReport: (metric: LCPMetric) => void, opts: ReportOpts =
9696
// task to reduce potential INP impact.
9797
// https://github.com/GoogleChrome/web-vitals/issues/383
9898
whenIdleOrHidden(stopListening);
99-
if (WINDOW.document) {
100-
removeEventListener(event.type, stopListeningWrapper, {
101-
capture: true,
102-
});
103-
}
99+
removePageListener(event.type, stopListeningWrapper, {
100+
capture: true,
101+
});
104102
}
105103
};
106104

@@ -109,11 +107,9 @@ export const onLCP = (onReport: (metric: LCPMetric) => void, opts: ReportOpts =
109107
// unreliable since it can be programmatically generated.
110108
// See: https://github.com/GoogleChrome/web-vitals/issues/75
111109
for (const type of ['keydown', 'click', 'visibilitychange']) {
112-
if (WINDOW.document) {
113-
addEventListener(type, stopListeningWrapper, {
114-
capture: true,
115-
});
116-
}
110+
addPageListener(type, stopListeningWrapper, {
111+
capture: true,
112+
});
117113
}
118114
}
119115
});

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import { WINDOW } from '../../../types';
1818
import { getActivationStart } from './getActivationStart';
19+
import { addPageListener, removePageListener } from './globalListeners';
1920

2021
let firstHiddenTime = -1;
2122
const onHiddenFunctions: Set<() => void> = new Set();
@@ -53,7 +54,7 @@ const onVisibilityUpdate = (event: Event) => {
5354
// We no longer need the `prerenderingchange` event listener now we've
5455
// set an initial init time so remove that
5556
// (we'll keep the visibilitychange one for onHiddenFunction above)
56-
WINDOW.document?.removeEventListener('prerenderingchange', onVisibilityUpdate, true);
57+
removePageListener('prerenderingchange', onVisibilityUpdate, true);
5758
}
5859
}
5960
};
@@ -76,17 +77,17 @@ export const getVisibilityWatcher = () => {
7677
// Listen for visibility changes so we can handle things like bfcache
7778
// restores and/or prerender without having to examine individual
7879
// timestamps in detail and also for onHidden function calls.
79-
WINDOW.document?.addEventListener('visibilitychange', onVisibilityUpdate, true);
80+
addPageListener('visibilitychange', onVisibilityUpdate, true);
8081

81-
// Some browsers have buggy implementations of visibilitychange,
82-
// so we use pagehide in addition, just to be safe.
83-
WINDOW.document?.addEventListener('pagehide', onVisibilityUpdate, true);
82+
// // Some browsers have buggy implementations of visibilitychange,
83+
// // so we use pagehide in addition, just to be safe.
84+
// addPageListener('pagehide', onVisibilityUpdate, true);
8485

8586
// IMPORTANT: when a page is prerendering, its `visibilityState` is
8687
// 'hidden', so in order to account for cases where this module checks for
8788
// visibility during prerendering, an additional check after prerendering
8889
// completes is also required.
89-
WINDOW.document?.addEventListener('prerenderingchange', onVisibilityUpdate, true);
90+
addPageListener('prerenderingchange', onVisibilityUpdate, true);
9091
}
9192

9293
return {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { WINDOW } from '../../../types';
2+
3+
/**
4+
* web-vitals 5.1.0 switched listeners to be added on the window rather than the document.
5+
* Instead of having to check for window/document every time we add a listener, we can use this function.
6+
*/
7+
export function addPageListener(type: string, listener: EventListener, options?: boolean | AddEventListenerOptions) {
8+
if (WINDOW.document) {
9+
WINDOW.addEventListener(type, listener, options);
10+
}
11+
}
12+
/**
13+
* web-vitals 5.1.0 switched listeners to be removed from the window rather than the document.
14+
* Instead of having to check for window/document every time we remove a listener, we can use this function.
15+
*/
16+
export function removePageListener(type: string, listener: EventListener, options?: boolean | AddEventListenerOptions) {
17+
if (WINDOW.document) {
18+
WINDOW.removeEventListener(type, listener, options);
19+
}
20+
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { WINDOW } from '../../../types';
18+
import { addPageListener } from './globalListeners';
1819

1920
export interface OnHiddenCallback {
2021
(event: Event): void;
@@ -37,10 +38,8 @@ export const onHidden = (cb: OnHiddenCallback) => {
3738
}
3839
};
3940

40-
if (WINDOW.document) {
41-
addEventListener('visibilitychange', onHiddenOrPageHide, true);
42-
// Some browsers have buggy implementations of visibilitychange,
43-
// so we use pagehide in addition, just to be safe.
44-
addEventListener('pagehide', onHiddenOrPageHide, true);
45-
}
41+
addPageListener('visibilitychange', onHiddenOrPageHide, true);
42+
// Some browsers have buggy implementations of visibilitychange,
43+
// so we use pagehide in addition, just to be safe.
44+
addPageListener('pagehide', onHiddenOrPageHide, true);
4645
};

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { WINDOW } from '../../../types.js';
18+
import { addPageListener, removePageListener } from './globalListeners.js';
1819
import { onHidden } from './onHidden.js';
1920
import { runOnce } from './runOnce.js';
2021

@@ -32,12 +33,12 @@ export const whenIdleOrHidden = (cb: () => void) => {
3233
} else {
3334
// eslint-disable-next-line no-param-reassign
3435
cb = runOnce(cb);
35-
WINDOW.document?.addEventListener('visibilitychange', cb, { once: true, capture: true });
36+
addPageListener('visibilitychange', cb, { once: true, capture: true });
3637
rIC(() => {
3738
cb();
3839
// Remove the above event listener since no longer required.
3940
// See: https://github.com/GoogleChrome/web-vitals/issues/622
40-
WINDOW.document?.removeEventListener('visibilitychange', cb, { capture: true });
41+
removePageListener('visibilitychange', cb, { capture: true });
4142
});
4243
// sentry: we use onHidden instead of directly listening to visibilitychange
4344
// because some browsers we still support (Safari <14.4) don't fully support

0 commit comments

Comments
 (0)