Skip to content

Commit 019cf99

Browse files
committed
fix: listen on page hide as i have guessed
1 parent eda7072 commit 019cf99

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ const initHiddenTime = () => {
3333
const onVisibilityUpdate = (event: Event) => {
3434
// Handle changes to hidden state
3535
if (isPageHidden(event) && firstHiddenTime > -1) {
36-
if (event.type === 'visibilitychange') {
36+
// Sentry-specific change: Also call onHidden callbacks for pagehide events
37+
// to support older browsers (Safari <14.4) that don't properly fire visibilitychange
38+
if (event.type === 'visibilitychange' || event.type === 'pagehide') {
3739
for (const onHiddenFunction of onHiddenFunctions) {
3840
onHiddenFunction();
3941
}
@@ -46,14 +48,14 @@ const onVisibilityUpdate = (event: Event) => {
4648
// visible prior to this change, so the event timestamp is the first
4749
// hidden time.
4850
// However, if the event is not a 'visibilitychange' event, then it must
49-
// be a 'prerenderingchange' event, and the fact that the document is
51+
// be a 'prerenderingchange' or 'pagehide' event, and the fact that the document is
5052
// still 'hidden' from the above check means the tab was activated
5153
// in a background state and so has always been hidden.
5254
firstHiddenTime = event.type === 'visibilitychange' ? event.timeStamp : 0;
5355

5456
// We no longer need the `prerenderingchange` event listener now we've
5557
// set an initial init time so remove that
56-
// (we'll keep the visibilitychange one for onHiddenFunction above)
58+
// (we'll keep the visibilitychange and pagehide ones for onHiddenFunction above)
5759
removePageListener('prerenderingchange', onVisibilityUpdate, true);
5860
}
5961
}
@@ -79,9 +81,10 @@ export const getVisibilityWatcher = () => {
7981
// timestamps in detail and also for onHidden function calls.
8082
addPageListener('visibilitychange', onVisibilityUpdate, true);
8183

82-
// // Some browsers have buggy implementations of visibilitychange,
83-
// // so we use pagehide in addition, just to be safe.
84-
// addPageListener('pagehide', onVisibilityUpdate, true);
84+
// Sentry-specific change: Some browsers have buggy implementations of visibilitychange,
85+
// so we use pagehide in addition, just to be safe. This is also required for older
86+
// Safari versions (<14.4) that we still support.
87+
addPageListener('pagehide', onVisibilityUpdate, true);
8588

8689
// IMPORTANT: when a page is prerendering, its `visibilityState` is
8790
// 'hidden', so in order to account for cases where this module checks for

0 commit comments

Comments
 (0)