Bug 199854 - Safari on iOS doesn't fire any events when the tab/app is closed
Summary: Safari on iOS doesn't fire any events when the tab/app is closed
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: Safari 12
Hardware: iPhone / iPad All
: P2 Major
Assignee: Nobody
URL:
Keywords: InRadar
: 213770 (view as bug list)
Depends on:
Blocks:
 
Reported: 2019-07-17 04:20 PDT by Jason Gissing
Modified: 2023-04-17 07:16 PDT (History)
9 users (show)

See Also:


Attachments
test page demonstrating the issue (2.28 KB, text/html)
2019-07-17 04:20 PDT, Jason Gissing
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jason Gissing 2019-07-17 04:20:48 PDT
Created attachment 374286 [details]
test page demonstrating the issue

On iOS, the document's visibilitychange event is fired when changing tab, but not when a tab is closed, or if the Safari app is suspended/killed.
pagehide, beforeunload & unload all seem not to fire at all on iOS.


We have an application which sets up sessions for clients, but we seem to have no way of detecting that a client has (even possibly) gone away, and so sessions are not cleaned up.

Safari on desktop does not have these issues, and neither do browsers on Android.


Most related bugs seem to be linked to fault https://bugs.webkit.org/show_bug.cgi?id=116769, but this has had no activity since 2013.



Attached is a simple test page, which tries to send a beacon to a publicly accessible endpoint (it has daily limits, so change the endpoint to your own if need be) on pagehide, unload, beforeunload & visibilitychange events. None of these are fired if the tab or app is closed.
Comment 1 Radar WebKit Bug Importer 2019-07-19 23:51:07 PDT
<rdar://problem/53339572>
Comment 2 Antoine Quint 2019-08-12 09:06:05 PDT
I think this behaves correctly, but someone else should advise on whether that is the case. Cc'ing Ryosuke who may know.
Comment 3 Ryosuke Niwa 2019-08-13 16:37:14 PDT
If we're not firing page hide when a tab / MobileSafari is closed explicitly by the user, that would be a WebKit bug.

If WebContent process dies (e.g. due to jetsam / crash) or MobileSafari itself is killed, then the fact pagehide event not firing is by design. If there is no process left, or we ran out of memory, then we can't run any scripts.
Comment 4 Simon Lieschke 2020-02-28 15:47:06 PST
FWIW I've also observed with the test page that none of the events are fired when navigating to another page in the same tab. Curiously however if the test page is reload a pagehide and an unload event do get fired. (Observed with Safari 13 on iOS 13.3.1.)
Comment 5 Alexey Proskuryakov 2020-07-01 09:27:56 PDT
*** Bug 213770 has been marked as a duplicate of this bug. ***
Comment 6 max 2023-04-17 07:16:26 PDT
I can confirm that none of the following event listeners are triggered when a tab is closed in iOS Safari. Tested on iOS 16.3.1.

addEventListener('visibilitychange', trackEvent, true);
addEventListener('pagehide', trackEvent, true);
addEventListener('freeze', trackEvent, true);
addEventListener('resume', trackEvent, true);
addEventListener('beforeunload', trackEvent, true);
addEventListener('unload', trackEvent, true);
document.addEventListener('beforeunload', trackEvent, true);
document.addEventListener('unload', trackEvent, true);
document.body.addEventListener('beforeunload', trackEvent, true);
document.body.addEventListener('unload', trackEvent, true);
window.onunload = trackEvent;

This means there's absolutely no way we can use navigator.sendBeacon to send analytics data to our backend when a user closes the tab. Same issue when the browser is force killed on the iPhone (i.e. Safari is force closed rather than put in the background).

The most closely related issue I can find is this: https://bugs.webkit.org/show_bug.cgi?id=151610