super-fields icon indicating copy to clipboard operation
super-fields copied to clipboard

UnloadObserver should know whether the user stayed on the page after prompt

Open vaadin-miki opened this issue 5 years ago • 5 comments
trafficstars

#170 allows querying the user before leaving the page, but there is currently no way to find out whether or not the user stayed on the page (because unload events have no connection to the server).

There probably is a way to find it out, and it would be nice to have it implemented in UnloadObserver.

vaadin-miki avatar Jun 26 '20 21:06 vaadin-miki

Did you also try using the Beacon API in an unload event handler, as suggested by @Legioth on Vaadin Forum?

mperktold avatar Jul 23 '20 14:07 mperktold

I guess using the Beacon API for this is hard because there is no easy way to expose an endpoint to which the Beacon request could be sent.

Probably it would be better/easier to integrate this feature into Vaadin Flow itself. The client could send the events to the same endpoint and in the same format to the server as usual, only using the Beacon API instead of fetch or XHR.

Maybe this could even be enabled by default and the server should close the UI in response to the unload event. Then the framework would automatically call the component's onDetach hook, so resource cleanup on tab close or refresh would just work out of the box.

mperktold avatar Jul 27 '20 10:07 mperktold

@mperktold thanks for feedback! I looked at the Beacon API and there were two reasons why I did not do any further work; my JS skills are less than I would want them to be, plus as you noticed, there is no easy way to ping the server

if you believe this should be a built-in feature of flow, feel free to create a ticket in vaadin/flow repository, but as for now there are no plans to introduce any functionality like this (simply because the usage would be marginal)

vaadin-miki avatar Jul 27 '20 10:07 vaadin-miki

I've created an actual running example based on my idea with the Beacon API. See https://vaadin.com/forum/thread/17523194/18399446 for more details.

Legioth avatar Aug 13 '20 08:08 Legioth

Great! I will adapt this code in one of the next releases.

vaadin-miki avatar Aug 24 '20 18:08 vaadin-miki