Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Navigate with full url #20846

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,11 @@ function Flow() {
(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 });
},
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());
}

}
Loading