From 0577cba1bfd1ce45af8676f5c24fb8459176703e Mon Sep 17 00:00:00 2001 From: Raju Ahmed Date: Thu, 9 Oct 2025 16:16:27 +0600 Subject: [PATCH] [FSSDK-11879] use visibilitychange event for page hide detection --- lib/index.browser.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/index.browser.ts b/lib/index.browser.ts index 0f644a844..ffa291f33 100644 --- a/lib/index.browser.ts +++ b/lib/index.browser.ts @@ -33,13 +33,18 @@ export const createInstance = function(config: Config): Client { }); if (client) { - const unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload'; - window.addEventListener( - unloadEvent, - () => { + if ('onvisibilitychange' in document) { + document.addEventListener('visibilitychange', () => { + if (document.hidden) { + client.flushImmediately(); + } + }); + } else { + const unloadEvent = 'onpagehide' in window ? 'pagehide' : 'unload'; + window.addEventListener(unloadEvent, () => { client.flushImmediately(); - }, - ); + }); + } } return client;