FMA possible reliable way to signal a browser session end to Wt
Added by Filipe Marques 2 months ago
Currently when closing a tab//browser or refresh the session will remain until the session timeout kills/ends it.
However according to JS docs, there might be 2 ways to send a request and ensure it completes even if the tab goes away:
Beacon API: https://developer.mozilla.org/en-US/docs/Web/API/Beacon_API:
"The Beacon API is used to send an asynchronous and non-blocking request to a web server. The request does not expect a response. Unlike requests made using XMLHttpRequest or the Fetch API, the browser guarantees to initiate beacon requests before the page is unloaded and to run them to completion.".
Or even using Fetch API with the keepalive property: https://developer.mozilla.org/en-US/docs/Web/API/Request/keepalive:
"The keepalive read-only property of the Request interface contains the request's keepalive setting (true or false), which indicates whether the browser will keep the associated request alive if the page that initiated it is unloaded before the request is complete."
window.addEventListener('unload', () => {
fetch("/session_end_funload", {
method: 'POST',
keepalive: true
});
navigator.sendBeacon('/session_end_bunload', '');
});
window.addEventListener('beforeunload', () => {
fetch("/session_end_fbunload", {
method: 'POST',
keepalive: true
});
navigator.sendBeacon('/session_end_bbunload', '');
});
I might be wrong, would this not be a solution to this problem?
Replies (3)
RM RE: A possible reliable way to signal a browser session end to Wt - Added by Romain Mardulyn 26 days ago
Hi Filipe,
This will still not be 100% reliable but would clearly be more reliable that what we are doing now. I made Issue #14709 to track this improvement.
FM RE: A possible reliable way to signal a browser session end to Wt - Added by Filipe Marques 26 days ago
What are the situations when this may not work? I tested closing the browser and only the beforeunload ones triggered.
RM RE: A possible reliable way to signal a browser session end to Wt - Added by Romain Mardulyn 22 days ago
When you spam F5 for instance. Also, if a phone user switches app, and then closes the browser, the signal will not be fired.