@@ -258,6 +258,16 @@ export default function Home({ }) {
258258 } , [ session ?. token ?. email ] )
259259
260260
261+ useEffect ( ( ) => {
262+ const handlePageClose = ( ) => {
263+ window . isPageClosing = true ;
264+ // Reset flag if unload is cancelled by another handler
265+ setTimeout ( ( ) => { window . isPageClosing = false ; } , 0 ) ;
266+ } ;
267+ window . addEventListener ( 'beforeunload' , handlePageClose ) ;
268+ return ( ) => window . removeEventListener ( 'beforeunload' , handlePageClose ) ;
269+ } , [ ] )
270+
261271 // this breaks stuff like logout and set username reloads
262272 // useEffect(() => {
263273 // window.onbeforeunload = function(e) {
@@ -1804,7 +1814,7 @@ export default function Home({ }) {
18041814 ws . onclose = ( ) => {
18051815 setWs ( null )
18061816 console . log ( "ws closed" )
1807- sendEvent ( "multiplayer_disconnect" )
1817+ if ( ! window . isPageClosing ) sendEvent ( "multiplayer_disconnect" )
18081818 setMultiplayerState ( ( prev ) => ( {
18091819 ...initialMultiplayerState ,
18101820 maxRetries : prev . maxRetries ,
@@ -1828,7 +1838,7 @@ export default function Home({ }) {
18281838 ws . onerror = ( ) => {
18291839 setWs ( null )
18301840 console . log ( "ws error" )
1831- sendEvent ( "multiplayer_disconnect" )
1841+ if ( ! window . isPageClosing ) sendEvent ( "multiplayer_disconnect" )
18321842
18331843 setMultiplayerState ( ( prev ) => ( {
18341844 ...initialMultiplayerState ,
0 commit comments