|
16 | 16 |
|
17 | 17 | import { WINDOW } from '../../../types';
|
18 | 18 |
|
19 |
| -// sentry: This function is used in web-vitals 4.2.4 but we continue |
20 |
| -// to use it in our v5 version. We still need it for the FID logic |
21 |
| -// and one internal use case (TBD if we switch over) in both. |
22 |
| -export const onHidden = (cb: () => void) => { |
23 |
| - WINDOW.document?.addEventListener('visibilitychange', () => { |
24 |
| - if (WINDOW.document?.visibilityState === 'hidden') { |
25 |
| - cb(); |
| 19 | +export interface OnHiddenCallback { |
| 20 | + (event: Event): void; |
| 21 | +} |
| 22 | + |
| 23 | +// Sentry-specific change: |
| 24 | +// This function's logic was NOT updated to web-vitals 4.2.4 or 5.x but we continue |
| 25 | +// to use the web-vitals 3.5.2 versiondue to us having stricter browser support. |
| 26 | +// PR with context that made the changes: https://github.com/GoogleChrome/web-vitals/pull/442/files#r1530492402 |
| 27 | +// The PR removed listening to the `pagehide` event, in favour of only listening to `visibilitychange` event. |
| 28 | +// This is "more correct" but some browsers we still support (Safari <14.4) don't fully support `visibilitychange` |
| 29 | +// or have known bugs w.r.t the `visibilitychange` event. |
| 30 | +// TODO (v10): If we decide to drop support for Safari 14.4, we can use the logic from web-vitals 4.2.4 |
| 31 | +// In this case, we also need to update the integration tests that currently trigger the `pagehide` event to |
| 32 | +// simulate the page being hidden. |
| 33 | +export const onHidden = (cb: OnHiddenCallback) => { |
| 34 | + const onHiddenOrPageHide = (event: Event) => { |
| 35 | + if (event.type === 'pagehide' || WINDOW.document?.visibilityState === 'hidden') { |
| 36 | + cb(event); |
26 | 37 | }
|
27 |
| - }); |
| 38 | + }; |
| 39 | + |
| 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 | + } |
28 | 46 | };
|
0 commit comments