Browser tab close listener on Vaadin Flow


I have to resolve resources (server) if a user leaves a page (Browser tab).

If the user moves around the page, DetachNotifier.addDetachListener works great. If he closes the whole browser, I can use VaadinService.addSessionDestroyListener.

But how can I catch if a user closes the browser tab, even without closing other tabs of the same application?

Thanks and greetings,

Seems like it is not possible:

Probably not reliable, but better than nothing for watching closed / refreshed browser tabs:

The Route defines a layout class, e.g. @Route(value = "", layout = MyLayout.class).
This layout class can be extended with implements DetachNotifier and, in the constructor, a

addDetachListener(e -> removeAll()); // Called if tab is closed / reloaded and heartbeat stopped. Not fully reliable

removeAll() triggers the detachListener of all contained Components and makes sure it won’t be called twice.

If this doesn’t work, SessionDestroyListener gotta do the job.

Now is working…

// sample Page with onbeforeunload script
public class SomeView extends Span implements PageConfigurator {

    public VaadinApplicationView() {
    public void configurePage(InitialPageSettings settings) {
        String script = "window.onbeforeunload = function (e) { var e = e || window.event; document.getElementById(\"SomeView\").$server.browserIsLeaving(); return; };";		
        settings.addInlineWithContents(InitialPageSettings.Position.PREPEND, script, InitialPageSettings.WrapMode.JAVASCRIPT);
    public void browserIsLeaving() {
        System.out.println("Called browserIsLeaving");