Skip to content

Commit a4911ab

Browse files
committed
Use pagehide event by default to avoid Chrome deprecation warnings
Replace deprecated unload event with pagehide for better bfcache compatibility and to eliminate Chrome warnings. Fallback to beforeunload/unload for older browsers. See #285
1 parent c63c144 commit a4911ab

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

modules/javascript/src/main/webapp/javascript/atmosphere.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@
205205
curWebsocketErrorRetries: 0,
206206
unloadBackwardCompat: !navigator.sendBeacon,
207207
useBeforeUnloadForCleanup: true,
208+
usePageHide: true,
208209
id: undefined,
209210
openId: undefined,
210211
reconnectId: undefined,
@@ -3466,6 +3467,15 @@
34663467
_beforeUnloadState = false;
34673468
}, 5000);
34683469
},
3470+
pageHide: function (event) {
3471+
atmosphere.util.debug(new Date() + " Atmosphere: pagehide event");
3472+
3473+
// Only cleanup if page is being persisted to bfcache or actually unloading
3474+
// event.persisted indicates if page is entering bfcache
3475+
if (requests.length > 0) {
3476+
atmosphere.unsubscribe();
3477+
}
3478+
},
34693479
offline: function () {
34703480
atmosphere.util.debug(new Date() + " Atmosphere: offline event");
34713481
offline = true;
@@ -3499,15 +3509,30 @@
34993509
};
35003510

35013511
atmosphere.bindEvents = function () {
3502-
atmosphere.util.on(window, "unload", atmosphere.callbacks.unload);
3503-
atmosphere.util.on(window, "beforeunload", atmosphere.callbacks.beforeUnload);
3512+
var request = requests.length > 0 ? requests[0].request : {};
3513+
3514+
// Use pagehide as the default modern approach (doesn't block bfcache)
3515+
if (request.usePageHide !== false && 'onpagehide' in window) {
3516+
atmosphere.util.on(window, "pagehide", atmosphere.callbacks.pageHide);
3517+
}
3518+
// Fallback to beforeunload if pagehide is disabled or not supported
3519+
else if (request.useBeforeUnloadForCleanup !== false) {
3520+
atmosphere.util.on(window, "beforeunload", atmosphere.callbacks.beforeUnload);
3521+
}
3522+
// Legacy unload event only if both modern options are disabled
3523+
else {
3524+
atmosphere.util.on(window, "unload", atmosphere.callbacks.unload);
3525+
}
3526+
35043527
atmosphere.util.on(window, "offline", atmosphere.callbacks.offline);
35053528
atmosphere.util.on(window, "online", atmosphere.callbacks.online);
35063529
};
35073530

35083531
atmosphere.unbindEvents = function () {
3509-
atmosphere.util.off(window, "unload", atmosphere.callbacks.unload);
3532+
// Unbind all possible unload-related events to ensure proper cleanup
3533+
atmosphere.util.off(window, "pagehide", atmosphere.callbacks.pageHide);
35103534
atmosphere.util.off(window, "beforeunload", atmosphere.callbacks.beforeUnload);
3535+
atmosphere.util.off(window, "unload", atmosphere.callbacks.unload);
35113536
atmosphere.util.off(window, "offline", atmosphere.callbacks.offline);
35123537
atmosphere.util.off(window, "online", atmosphere.callbacks.online);
35133538
};

0 commit comments

Comments
 (0)