flow-components icon indicating copy to clipboard operation
flow-components copied to clipboard

Inconsistent behavior when forwarding to a view vs refresh

Open OlliTietavainenVaadin opened this issue 3 years ago • 7 comments

Description of the bug

Two example views: HelloWorldView and AboutView. AboutView accepts optional parameters. HelloWorldView forwards to AboutView with a String parameter.

@Route(value = "hello", layout = MainLayout.class)
public class HelloWorldView extends HorizontalLayout implements BeforeEnterObserver {

    @Override
    public void beforeEnter(BeforeEnterEvent event) {
        event.forwardTo("about/123");
    }
}
@Route(value = "about", layout = MainLayout.class)
public class AboutView extends VerticalLayout implements HasUrlParameter<String> {

    @Override
    public void setParameter(BeforeEvent event, @OptionalParameter String parameter) {
        Notification.show("Got parameter: " + parameter);
    }
}

If I navigate first to /hello, the URL gets updated to /about/123 but no Notification is shown. If I refresh the page with /about/123 in the URL bar, I get the notification "Got parameter: 123". I would expect the view's parameterized status to be the same if the URL doesn't change.

Expected behavior

The setParameter should fire even after the forwardTo with the needed parameter values.

Minimal reproducible example

v23-2-start.zip

Versions

  • Vaadin / Flow version: 23.2.0
  • Java version: n/a
  • OS version: n/a
  • Browser version (if applicable): n/a
  • Application Server (if applicable): n/a
  • IDE (if applicable):

OlliTietavainenVaadin avatar Sep 19 '22 12:09 OlliTietavainenVaadin

@OlliTietavainenVaadin, if you use a breakpoint or sysout, I'm sure you'll see that setParameter(...) actually is called. The issue here is that a forward (via a BeforeEvent) seems to cancel any notifications.

For me it's notifications that I open before the forward (from within setParameter(...)). But trying it with your example, even notifications after the forward (but still within the same request) don't work. This started with Vaadin 23.2.0. 23.1.11 still works.

// Basically, when someone enters a view and encounters an error, I'll forward to the main view. But I'll show a notification so
// the user knows what's going on. After upgrading to 23.2.4 the notifications just stopped showing (which took a while to
// notice).

@Override
public void setParameter(BeforeEvent event, @OptionalParameter String parameter)
{
  Notification.show("Notification");
  event.forwardTo(OtherView.class);
}

Frettman avatar Oct 12 '22 13:10 Frettman

Wrapping the notification in the following seems to be a suitable workaround:

event.getUI().access(() ->
{
  Notification.show("Notification");
});

Frettman avatar Oct 12 '22 13:10 Frettman

I've got a similar configuration, but with beforeEnter instead of setParameter and with some Span instances added to a VerticalLayout instead of a notification. In this case too the method is called, but the elements don't show up on the client side unless the page is reloaded. So this is not only an issue with notifications.

cim-cpe avatar Nov 16 '22 09:11 cim-cpe

We are facing the same issue. The workaround https://github.com/vaadin/flow-components/issues/7229 seams to be working fine

davidef avatar Jul 12 '23 08:07 davidef

I've run into this issue as well.

FollowSteph avatar Sep 24 '23 20:09 FollowSteph

I can reproduce the Notification issue in the latest version. However, the case described in this comment works fine for me; I can see components added to the view and shown in the browser.

mcollovati avatar Mar 20 '25 11:03 mcollovati

The problem seems to be that the base overlay has a AfterNavigationListener that removes the notification opening action.

https://github.com/vaadin/flow-components/blame/b88ebf11ffa223579c494a722c5c109ad4370f40/vaadin-flow-components-shared-parent/vaadin-flow-components-base/src/main/java/com/vaadin/flow/component/shared/internal/OverlayAutoAddController.java#L76-L84

@sissbruecker suggested that the issue could be related to this change: https://github.com/vaadin/flow-components/pull/3602

Most likely, Notifications needs some specific handling.

I'll transfer the issue to flow-components repository for additional investigation

mcollovati avatar Mar 20 '25 11:03 mcollovati