flow icon indicating copy to clipboard operation
flow copied to clipboard

Page.setLocation() stops the application if navigation was canceled

Open cdir opened this issue 2 years ago • 1 comments

Description of the bug / feature

If I use Page.setLocation() (introduced in #4869) to navigate to an external API, the browser might block navigation with window.onbeforeunload. We use this to ask the user if they really want to leave the unsaved page.

In this case, the Vaadin application stops working. I think this is because page.open calls stopApplication in js.

Minimal reproducible example

Simple JavaScript

window.onbeforeunload = function() {
     return '';
}

Simple Button

Button button = new Button("go");
button.addClickListener(e -> UI.getCurrent().getPage().setLocation("https://vaadin.com"));
add(button);

Expected behavior

When a navigation is blocked the application should work as before.

Actual behavior

The application is broken.

Versions:

Tested with 23.1.3

cdir avatar Jul 14 '22 09:07 cdir

As mentioned in the issue public void setLocation(String uri) method of Page.java is doing the following:

public void setLocation(String uri) {
        this.open(uri, "_self");
}

public void open(String url, String windowName) {
        this.executeJs("if ($1 == '_self') this.stopApplication(); window.open($0, $1)", url, windowName);
}

And when the window.onbeforeunload opens the popup, the application has already stopped. Unfortunately, there is no proper callback for cancel option of the popup, so the user custom code cannot start the application again.

taefi avatar Aug 03 '22 10:08 taefi