diff --git a/apps/web/src/components/live/ControlBar/index.tsx b/apps/web/src/components/live/ControlBar/index.tsx index 036dfe0a..8be9f453 100644 --- a/apps/web/src/components/live/ControlBar/index.tsx +++ b/apps/web/src/components/live/ControlBar/index.tsx @@ -106,19 +106,24 @@ const ControlBar = ({ isOwner, onTicleEnd }: ControlBarProps) => { }; useEffect(() => { - const handleBeforeUnload = (e: Event) => { - if (isOwner) { - const blob = new Blob([], { - type: 'application/json', - }); + const handleBeforeUnload = (e: BeforeUnloadEvent) => { + e.preventDefault(); + }; + const handleUnload = () => { + if (isOwner) { + const blob = new Blob([], { type: 'application/json' }); navigator.sendBeacon(`${ENV.API_URL}/dashboard/${ticleId}/end`, blob); } - e.preventDefault(); }; window.addEventListener('beforeunload', handleBeforeUnload); - return () => window.removeEventListener('beforeunload', handleBeforeUnload); + window.addEventListener('unload', handleUnload); + + return () => { + window.removeEventListener('beforeunload', handleBeforeUnload); + window.removeEventListener('unload', handleUnload); + }; }, []); return ( diff --git a/apps/web/src/hooks/mediasoup/useMediasoup.ts b/apps/web/src/hooks/mediasoup/useMediasoup.ts index e3e7db48..9c37157e 100644 --- a/apps/web/src/hooks/mediasoup/useMediasoup.ts +++ b/apps/web/src/hooks/mediasoup/useMediasoup.ts @@ -102,11 +102,11 @@ const useMediasoup = () => { clearMediasoup(); }; - window.addEventListener('beforeunload', clearAll); + window.addEventListener('unload', clearAll); return () => { clearAll(); - window.removeEventListener('beforeunload', clearAll); + window.removeEventListener('unload', clearAll); }; }, []); };