cancel
Showing results for 
Search instead for 
Did you mean: 

How to implement "Beforeunload" in a SAPUI5 app?

massimiliano_cardosi
Product and Topic Expert
Product and Topic Expert
795

For accomplishing business requirement, we need to catch when end user closes his session, meaning: when he navigate to another external url; closes window/tab; closes browser itself. Logout and timeout are already handled properly

As per my findings, "onbeforeunload" function is already used by SAPUI5 library, so either I replace it with my one or find another solution

Many thanks

Massimiliano

Accepted Solutions (0)

Answers (1)

Answers (1)

boghyon
Product and Topic Expert
Product and Topic Expert
0 Kudos

Neither `unload` nor `beforeunload` should be used as an end-of-session signal. The former one is deprecated and the `beforeunload` is applicable only for notifying the user about unsaved changes. I'd suggest implementing handlers based on the guidelines Page Lifecycle API | Web Platform | Chrome for Developers especially the diagram from the section "Overview of Page Lifecycle states and events".

When sending an end-of-session signal, the request should not be based on XMLHttpRequest (XHR). The HTML specification doesn't guarantee that XHRs are sent reliably in a page dismissal or visibility change event. The replacement for XHR in such events is the Beacon API:

> "[...] Unlike requests made using XMLHttpRequest [...], the browser guarantees to initiate beacon requests before the page is unloaded and to run them to completion."

Note that `navigator.sendBeacon` doesn't support anything other than POST, and requires the payload not to exceed 64 kB. In case more configuration on such requests is required, `fetch` API with the option `keepalive: true` should be used.

massimiliano_cardosi
Product and Topic Expert
Product and Topic Expert
0 Kudos

hi Boghyon, thanks for suggestions - I've already read and tried what mentioned under both links; point is that not all "end-of-session" events are handled, as an example: if end user changes URL this navigation outside application is not catch. Window/tab closure works, refresh event works - as per my tests, only "beforeunload" is triggered when URL is changed by end user.

Thanks
Massimiliano