|
52 | 52 | var _beforeUnloadState = false; |
53 | 53 |
|
54 | 54 | var atmosphere = { |
55 | | - version: "4.0.1", |
| 55 | + version: "4.0.2", |
56 | 56 | onError: function (response) { |
57 | 57 | }, |
58 | 58 | onClose: function (response) { |
|
204 | 204 | maxWebsocketErrorRetries: 1, |
205 | 205 | curWebsocketErrorRetries: 0, |
206 | 206 | unloadBackwardCompat: !navigator.sendBeacon, |
| 207 | + useBeforeUnloadForCleanup: true, |
207 | 208 | id: undefined, |
208 | 209 | openId: undefined, |
209 | 210 | reconnectId: undefined, |
|
3431 | 3432 | atmosphere.callbacks = { |
3432 | 3433 | unload: function () { |
3433 | 3434 | atmosphere.util.debug(new Date() + " Atmosphere: " + "unload event"); |
3434 | | - atmosphere.unsubscribe(); |
| 3435 | + // Check if we should use the old unload behavior or if beforeunload hasn't handled cleanup |
| 3436 | + var shouldCleanupInUnload = requests.length > 0 && |
| 3437 | + (requests[0].request.useBeforeUnloadForCleanup === false || !_beforeUnloadState); |
| 3438 | + |
| 3439 | + if (shouldCleanupInUnload) { |
| 3440 | + atmosphere.unsubscribe(); |
| 3441 | + } |
3435 | 3442 | }, |
3436 | 3443 | beforeUnload: function () { |
3437 | 3444 | atmosphere.util.debug(new Date() + " Atmosphere: " + "beforeunload event"); |
|
3443 | 3450 |
|
3444 | 3451 | // ATMOSPHERE-JAVASCRIPT-143: Delay reconnect to avoid reconnect attempts before an actual unload (we don't know if an unload will happen, yet) |
3445 | 3452 | _beforeUnloadState = true; |
| 3453 | + |
| 3454 | + // Check if we should cleanup in beforeunload (default behavior for better bfcache compatibility) |
| 3455 | + var shouldCleanupInBeforeUnload = requests.length > 0 && |
| 3456 | + requests[0].request.useBeforeUnloadForCleanup !== false; |
| 3457 | + |
| 3458 | + if (shouldCleanupInBeforeUnload) { |
| 3459 | + // Primary cleanup now happens here instead of in unload event |
| 3460 | + // This ensures compatibility with Chrome's bfcache and follows modern best practices |
| 3461 | + atmosphere.unsubscribe(); |
| 3462 | + } |
| 3463 | + |
3446 | 3464 | atmosphere._beforeUnloadTimeoutId = setTimeout(function () { |
3447 | 3465 | atmosphere.util.debug(new Date() + " Atmosphere: " + "beforeunload event timeout reached. Reset _beforeUnloadState flag"); |
3448 | 3466 | _beforeUnloadState = false; |
|
0 commit comments