Skip to content

Commit adbf6a3

Browse files
authored
fix: Query in path (#19087)
* fix: Query in path Fix mistaken add of query parameters into the route breaking route resolution when navigating thrhough link. part of #19080 * update path normalizing, fix server replace state to use location * change replace order
1 parent 5ccac95 commit adbf6a3

4 files changed

Lines changed: 64 additions & 15 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/component/UI.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ public void browserNavigate(BrowserNavigateEvent event) {
18121812
// acknowledge client, but cancel if session not open
18131813
serverConnected(
18141814
!getSession().getState().equals(VaadinSessionState.OPEN));
1815-
replaceStateIfDiffersAndNoReplacePending(event.route, trimmedRoute);
1815+
replaceStateIfDiffersAndNoReplacePending(event.route, location);
18161816
}
18171817
}
18181818

@@ -1822,15 +1822,15 @@ public void browserNavigate(BrowserNavigateEvent event) {
18221822
*
18231823
* @param route
18241824
* the event.route
1825-
* @param trimmedRoute
1826-
* the trimmed route
1825+
* @param location
1826+
* the location with the trimmed route
18271827
*/
18281828
private void replaceStateIfDiffersAndNoReplacePending(String route,
1829-
String trimmedRoute) {
1830-
if (!trimmedRoute.equals(route) && !getInternals()
1829+
Location location) {
1830+
if (!location.getPath().equals(route) && !getInternals()
18311831
.containsPendingJavascript("window.history.replaceState")) {
18321832
// See InternalRedirectHandler invoked via Router.
1833-
getPage().getHistory().replaceState(null, trimmedRoute);
1833+
getPage().getHistory().replaceState(null, location);
18341834
}
18351835
}
18361836

‎flow-server/src/main/resources/com/vaadin/flow/server/frontend/Flow.tsx‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ function vaadinRouterGlobalClickHandler(event) {
131131
}
132132

133133
// if none of the above, convert the click into a navigation event
134-
const {pathname, href, baseURI, search, hash} = anchor;
134+
const {href, baseURI, search, hash} = anchor;
135135
// Normalize away base from pathname. e.g. /react should remove base /view from /view/react
136-
let normalizedPathname = href.slice(0, baseURI.length) == baseURI ? href.slice(baseURI.length) : pathname;
137-
normalizedPathname = normalizedPathname.startsWith("/") ? normalizedPathname: "/" + normalizedPathname;
136+
let normalizedPathname = href.replace(search,'').replace(baseURI, '').replace(hash, '');
137+
normalizedPathname = normalizedPathname.startsWith("/") ? normalizedPathname : "/" + normalizedPathname;
138138
if (fireRouterEvent('go', {pathname: normalizedPathname, search, hash, clientNavigation: true})) {
139139
event.preventDefault();
140140
// for a click event, the scroll is reset to the top position.
@@ -158,9 +158,10 @@ function navigateEventHandler(event) {
158158
event.preventDefault();
159159
}
160160
// Normalize path against baseURI if href available.
161-
let normalizedPathname = event.detail.href && event.detail.href.slice(0, document.baseURI.length) == document.baseURI ?
162-
event.detail.href.slice(document.baseURI.length) : event.detail.pathname;
163-
normalizedPathname = normalizedPathname.startsWith("/") ? normalizedPathname: "/"+normalizedPathname;
161+
let normalizedPathname = event.detail.href ?
162+
event.detail.href.replace(event.detail.search ,'').replace(document.baseURI, '').replace(event.detail.hash, '') :
163+
event.detail.pathname;
164+
normalizedPathname = normalizedPathname.startsWith("/") ? normalizedPathname : "/" + normalizedPathname;
164165

165166
// @ts-ignore
166167
let matched = matchRoutes(Array.from(routes), normalizedPathname);
@@ -187,8 +188,11 @@ function navigateEventHandler(event) {
187188
navigation(path, {replace: false});
188189
},
189190
continue: () => {
190-
if(window.location.pathname !== event.detail.pathname) {
191-
window.history.pushState(window.history.state, '', event.detail.pathname);
191+
let path = event.detail.pathname;
192+
if(event.detail.search) path += event.detail.search;
193+
if(event.detail.hash) path += event.detail.hash;
194+
if(window.location.pathname !== path) {
195+
window.history.pushState(window.history.state, '', path);
192196
window.dispatchEvent(new PopStateEvent('popstate', {state: 'vaadin-router-ignore'}));
193197
}
194198
}

‎flow-tests/test-react-router/src/main/java/com/vaadin/flow/NavigationView.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.vaadin.flow.component.html.Div;
2121
import com.vaadin.flow.component.html.NativeButton;
2222
import com.vaadin.flow.component.html.Span;
23+
import com.vaadin.flow.router.QueryParameters;
2324
import com.vaadin.flow.router.Route;
2425
import com.vaadin.flow.router.RouterLink;
2526

@@ -28,6 +29,8 @@ public class NavigationView extends Div {
2829

2930
public static final String SERVER_ID = "server-navigation";
3031
public static final String ANCHOR_ID = "anchor-navigation";
32+
public static final String ANCHOR_QUERY_ID = "anchor-query-navigation";
33+
public static final String ROUTER_LINK_QUERY_ID = "router-link-query-navigation";
3134
public static final String ROUTER_LINK_ID = "router-link-navigation";
3235
public static final String POSTPONE_ID = "postpone-view-link";
3336

@@ -64,6 +67,17 @@ public NavigationView() {
6467
reactServerNavigation.setId(REACT_ID);
6568

6669
add(new Div(), reactAnchorNavigation, new Div(), reactServerNavigation);
70+
71+
RouterLink rlViewQuery = new RouterLink("AnchorQuery",
72+
AnchorView.class);
73+
rlViewQuery.setQueryParameters(QueryParameters.of("test", "value"));
74+
rlViewQuery.setId(ROUTER_LINK_QUERY_ID);
75+
add(new Div(), rlViewQuery);
76+
77+
Anchor anchorViewQuery = new Anchor(
78+
"com.vaadin.flow.AnchorView?test=anchor", "AnchorQuery");
79+
anchorViewQuery.setId(ANCHOR_QUERY_ID);
80+
add(new Div(), anchorViewQuery);
6781
}
6882

6983
}

‎flow-tests/test-react-router/src/test/java/com/vaadin/flow/NavigationIT.java‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.vaadin.flow.testutil.ChromeBrowserTest;
2727

2828
public class NavigationIT extends ChromeBrowserTest {
29-
3029
@Test
3130
public void testNavigation() {
3231
open();
@@ -293,4 +292,36 @@ public void testReactNavigationBrowserHistoryBack_serverNavigation() {
293292
$(SpanElement.class).first().getText());
294293
}
295294

295+
@Test
296+
public void testRouterLinkWithQueryNavigation() {
297+
open();
298+
299+
Assert.assertEquals("NavigationView",
300+
$(SpanElement.class).first().getText());
301+
302+
$(AnchorElement.class).id(NavigationView.ROUTER_LINK_QUERY_ID).click();
303+
Assert.assertEquals(
304+
"Exception on router-link navigation with query parameters",
305+
"AnchorView", $(SpanElement.class).first().getText());
306+
System.out.println(getDriver().getCurrentUrl());
307+
Assert.assertTrue("Query was missing in url",
308+
getDriver().getCurrentUrl().endsWith("?test=value"));
309+
}
310+
311+
@Test
312+
public void testAnchorWithQueryNavigation() {
313+
open();
314+
315+
Assert.assertEquals("NavigationView",
316+
$(SpanElement.class).first().getText());
317+
318+
$(AnchorElement.class).id(NavigationView.ANCHOR_QUERY_ID).click();
319+
Assert.assertEquals(
320+
"Exception on router-link navigation with query parameters",
321+
"AnchorView", $(SpanElement.class).first().getText());
322+
System.out.println(getDriver().getCurrentUrl());
323+
Assert.assertTrue("Query was missing in url",
324+
getDriver().getCurrentUrl().endsWith("?test=anchor"));
325+
}
326+
296327
}

0 commit comments

Comments
 (0)