Skip to content

Commit

Permalink
fix: perform server navigation roundtrip only when client side naviga…
Browse files Browse the repository at this point in the history
…tion is completed (#20187) (#20195)

Fixes #19822

Co-authored-by: Marco Collovati <[email protected]>
  • Loading branch information
vaadin-bot and mcollovati authored Oct 9, 2024
1 parent 48ab83e commit 419622e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ function Flow() {
let blockingPromise: any;
roundTrip.current = new Promise<void>((resolve,reject) => blockingPromise = {resolve:resolve,reject:reject});

// Do not skip server round-trip if navigation originates from a click on a link
// Proceed to the blocked location, unless the navigation originates from a click on a link.
// In that case continue with function execution and perform a server round-trip
if (navigated.current && !fromAnchor.current) {
blocker.proceed();
blockingPromise.resolve();
Expand Down Expand Up @@ -421,6 +422,9 @@ function Flow() {
}, [blocker.state, blocker.location]);

useEffect(() => {
if (blocker.state === 'blocked') {
return;
}
if (navigated.current) {
navigated.current = false;
fireNavigated(location.pathname,location.search);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2000-2024 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.vaadin.flow;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouterLink;

@Route("com.vaadin.flow.RouterLinkForwardingToParametersView")
public class RouterLinkForwardingToParametersView extends Div {

public RouterLinkForwardingToParametersView() {
RouterLink link = new RouterLink("Forwarding view",
ForwardingToParametersView.class);
link.setId("forwardViewLink");
add(link);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,27 @@ public void testSetParameterCalledOnlyOnceAfterForward() {
.filter(span -> span.getText().equals("setParameter"))
.count());
}

// Test for https://github.com/vaadin/flow/issues/19822
@Test
public void testRouterLinkSetParameterCalledOnlyOnceAfterForward() {
getDriver().get(getTestURL(getRootURL(),
"/view/com.vaadin.flow.RouterLinkForwardingToParametersView",
null));
$("a").id("forwardViewLink").click();

try {
waitUntil(arg -> driver.getCurrentUrl().endsWith(
"/view/com.vaadin.flow.ForwardTargetWithParametersView"));
} catch (TimeoutException e) {
Assert.fail("URL wasn't updated to expected one: "
+ "/view/com.vaadin.flow.ForwardTargetWithParametersView");
}

Assert.assertEquals("setParameter was called more than once", 1,
$(SpanElement.class).all().stream()
.filter(span -> span.getText().equals("setParameter"))
.count());
}

}

0 comments on commit 419622e

Please sign in to comment.