Skip to content

Commit 0577cba

Browse files
committed
[FSSDK-11879] use visibilitychange event for page hide detection
1 parent 99a4ca7 commit 0577cba

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

lib/index.browser.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ export const createInstance = function(config: Config): Client {
3333
});
3434

3535
if (client) {
36-
const unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload';
37-
window.addEventListener(
38-
unloadEvent,
39-
() => {
36+
if ('onvisibilitychange' in document) {
37+
document.addEventListener('visibilitychange', () => {
38+
if (document.hidden) {
39+
client.flushImmediately();
40+
}
41+
});
42+
} else {
43+
const unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload';
44+
window.addEventListener(unloadEvent, () => {
4045
client.flushImmediately();
41-
},
42-
);
46+
});
47+
}
4348
}
4449

4550
return client;

0 commit comments

Comments
 (0)