vaadin / flow

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

History.replaceState is triggering afterNavigation in vaadin 24 (in vaadin 23 it was not triggered) #19711

Closed davidef closed 4 weeks ago

davidef commented 1 month ago

Description of the bug

We use History.replaceState to update url query parameters to reflect current view filter state. In Vaadin 23 we do not get any navigation related event as we are not navigating anywhere as we are just updating the url state so that it represent the current view state. After upgrading to Vaadin 24 we are now getting AfterNavigation events that in our code triggers the rebinding (not included in the example below) of the filter fields leading to unexpected behaviour as those happen while the user may be writing in the fields.

Expected behavior

No navigation event are triggered (as in vaadin 23) as we are just replacing the current history state and not doing any effective navigation.

Minimal reproducible example

@Route(value = "after-navigation")
public class AfterNavigationTestView extends VerticalLayout implements AfterNavigationObserver {
    private static final long serialVersionUID = 1L;

    private Location lastLocation = null;

    public AfterNavigationTestView() {
        TextField textField = new TextField();
        textField.addValueChangeListener(e -> {
            History history = UI.getCurrent().getPage().getHistory();
            history.replaceState(null, new Location(lastLocation.getSegments(), QueryParameters.of("v", e.getValue())));
        });
        textField.setValueChangeMode(ValueChangeMode.LAZY);
        add(textField);
    }

    @Override
    public void afterNavigation(AfterNavigationEvent event) {
        if (lastLocation != null) {
            Notification.show("Unexpected after navigation");
        }
        lastLocation = event.getLocation();
    }
}

Writing something in the TextField should not trigger afterNavigation

Versions

mcollovati commented 1 month ago

Probably duplicate of https://github.com/vaadin/flow/issues/19613

caalador commented 4 weeks ago

Tested and should have been fixed with https://github.com/vaadin/flow/issues/19613