Skip to content

Commit

Permalink
fix: Navigate with full url
Browse files Browse the repository at this point in the history
Fix navigating with full url
for instance when adding a
query parameter at the end.

Fixes #19580
  • Loading branch information
caalador committed Jan 15, 2025
1 parent 3fe8592 commit 4d57abc
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@ function Flow() {
const vaadinNavigateEventHandler = useCallback((event: CustomEvent<{state: unknown, url: string, replace?: boolean, callback: boolean}>) => {
// @ts-ignore
window.Vaadin.Flow.navigation = true;
const path = '/' + event.detail.url;
// clean base uri away if for instance redirected to http://localhost/path/user?id=10
// else the whole http... will be appended to the url see #19580
const path = event.detail.url.startsWith(document.baseURI)
? '/' + event.detail.url.slice(document.baseURI.length)
: '/' + event.detail.url;
fromAnchor.current = false;
queuedNavigate(path, event.detail.callback, { state: event.detail.state, replace: event.detail.replace });
}, [navigate]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* 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.UI;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.NativeButton;
import com.vaadin.flow.component.page.Page;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.Route;

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

public static final String PARAM_BUTTON_ID = "setParameter";
public static final String QUERY_ID = "query";

public AddQueryParamView() {
NativeButton button = new NativeButton("Add URL Parameter", e -> {
updateUrlRequestParameter("test", "HELLO!");
});
button.setId(PARAM_BUTTON_ID);
add(button);
}

public void updateUrlRequestParameter(String key, String value) {
Page page = UI.getCurrent().getPage();
page.fetchCurrentURL(url -> {
String newLocation = url + "?" + key + "=" + value;
page.getHistory().replaceState(null, newLocation);
Div div = new Div(newLocation);
div.setId(QUERY_ID);
add(div);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.vaadin.flow;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;

import com.vaadin.flow.component.html.testbench.DivElement;
import com.vaadin.flow.component.html.testbench.NativeButtonElement;
import com.vaadin.flow.component.html.testbench.SpanElement;
import com.vaadin.flow.testutil.ChromeBrowserTest;

public class AddQueryParamIT extends ChromeBrowserTest {

@Test
public void validateReactInUse() {
open();

waitForDevServer();

$(NativeButtonElement.class).id(AddQueryParamView.PARAM_BUTTON_ID)
.click();

waitForElementPresent(By.id(AddQueryParamView.QUERY_ID));

Assert.assertEquals(
$(DivElement.class).id(AddQueryParamView.QUERY_ID).getText(),
driver.getCurrentUrl());
}

}

0 comments on commit 4d57abc

Please sign in to comment.