vaadin / flow

Vaadin Flow is a Java framework binding Vaadin web components to Java. This is part of Vaadin 10+.
Apache License 2.0
598 stars 165 forks source link

BeforeLeaveObserver not usable for the proposed purpose #14499

Open aley2003 opened 1 year ago

aley2003 commented 1 year ago

Description of the bug

From https://vaadin.com/docs/latest/routing/lifecycle: "A typical use case for this event is to ask the user whether they want to save any unsaved changes before navigating to another part of the application." For this use case the BeforeLeaveObserver is not usable because of (from interface BeforeLeaveObserver): "If a route target is left for reasons not under the control of the navigator (for instance using Page.setLocation(URI), typing a URL into the address bar, or closing the browser), listeners are not called." So the observer is not called reliable and there are many cases the view could be left without saving changes.

Expected behavior

BeforeLeaveObserver.beforeLeave should be called in any case before a View would be left.

Minimal reproducible example

Every view implementing BeforeLeaveObserver

Versions

aley2003 commented 1 year ago

In Vaadin 8 beforeLeave() was working reliable.

simasch commented 1 year ago

As leaving the View by closing the browser tab or similar is not under the control of Vaadin, this would probably not work with a BeforeLeaveObserver.

There is an old thread about this topic: https://vaadin.com/forum/thread/17523194/unsaved-changes-detect-page-exit-or-reload We more or less do what's described there.

BUT it would be great if Vaadin would support that out of the box.

knoobie commented 1 year ago

In Vaadin 8 beforeLeave() was working reliable.

That is not true. It is working the same as in V8. If you call window.open("http://www.google.de", "_self"); in a V8 app / view with a before leave observer is also not called.

aley2003 commented 1 year ago

But in V8 beforeLeave() was triggered by a change in the application URL (e.g. https://my-server/myApp/view1 to https://my-server/myApp/view2) at least.