Vaadin 8: Immediate event for a closed UI ?

I need to know when a page is closed – I have to clean some data structures attached to the corresponding UI and View objects (such as event bus subscriptions)

In Vaadin 6, I could register a CloseListener to the page (not only to pop-up browser Windows as is now the case).

In Vaadin 8, the closest I can see is “detach()” as explained in
https://vaadin.com/wiki/-/wiki/Main/Cleaning+up+resources+in+an+UI
but it seems peculiar to be notified after several minutes by default.
I also looked at the back/forward history support, in hope that a “going away listener” would be there for closing as well, but no dice (I was thinking of the javascript onbeforeunload hack).

The reason why it has been removed after version 6, is that it was not working reliably for all the browsers, and there was no alternative back then, since this information is not just available from the browser.

The
onunload
event is triggered when the browser window closes, but it is also triggered for refresh, navigating away from page…

You can setup the heartbeat interval to be lover to get the UI.detach triggered faster, as the UI is closed after three missed heartbeats (if I remember correctly).