Skip to content

Commit b271e74

Browse files
vaadin-botJohannes Eriksson
andauthored
fix: push '.' instead of '' when navigating (#12991) (#13018)
The API for Location::getPath/getPathWithQueryParameters was changed in V15 to return "" instead of "." for the base URL. However, it seems to not have been taken into account that FF / Safari do not navigate to base when passing the empty string for URL to the history methods (and this ticket, which seems to have observed that this workaround was necessary, was closed without action). This commit moves the workaround to its own method in History. Fixes #5113 Co-authored-by: Johannes Eriksson <joheriks@vaadin.com>
1 parent a764046 commit b271e74

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

flow-server/src/main/java/com/vaadin/flow/component/page/History.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ public void pushState(JsonValue state, String location) {
182182
* to only change the JSON state
183183
*/
184184
public void pushState(JsonValue state, Location location) {
185-
final String pathWithQueryParameters = Optional.ofNullable(location)
186-
.map(Location::getPathWithQueryParameters).orElse(null);
185+
final String pathWithQueryParameters = getPathWithQueryParameters(
186+
location);
187187
// Second parameter is title which is currently ignored according to
188188
// https://developer.mozilla.org/en-US/docs/Web/API/History_API
189189
ui.getPage().executeJs(
@@ -221,8 +221,8 @@ public void replaceState(JsonValue state, String location) {
221221
* to only change the JSON state
222222
*/
223223
public void replaceState(JsonValue state, Location location) {
224-
final String pathWithQueryParameters = Optional.ofNullable(location)
225-
.map(Location::getPathWithQueryParameters).orElse(null);
224+
final String pathWithQueryParameters = getPathWithQueryParameters(
225+
location);
226226
// Second parameter is title which is currently ignored according to
227227
// https://developer.mozilla.org/en-US/docs/Web/API/History_API
228228
ui.getPage().executeJs(
@@ -297,4 +297,13 @@ public void forward() {
297297
public void go(int steps) {
298298
ui.getPage().executeJs("history.go($0)", steps);
299299
}
300+
301+
private String getPathWithQueryParameters(Location location) {
302+
// In the Location API, getPath() returning "" means document base URL.
303+
// On FF and Safari, passing '' for the URL parameter does not update
304+
// URL to the base, so we replace '' with '.' here.
305+
return Optional.ofNullable(location)
306+
.map(Location::getPathWithQueryParameters)
307+
.map(path -> path.isEmpty() ? "." : path).orElse(null);
308+
}
300309
}

flow-server/src/test/java/com/vaadin/flow/component/page/HistoryTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,14 @@ public void replaceState_locationWithQueryParametersAndFragment_QueryParametersA
150150
page.parameters[1]);
151151
}
152152

153+
@Test // #11628
154+
public void replaceState_locationEmpty_pushesPeriod() {
155+
history.replaceState(null, "");
156+
Assert.assertEquals("push state JS not included",
157+
"setTimeout(() => window.history.replaceState($0, '', $1))",
158+
page.expression);
159+
Assert.assertEquals(null, page.parameters[0]);
160+
Assert.assertEquals("location should be '.'", ".", page.parameters[1]);
161+
}
162+
153163
}

0 commit comments

Comments
 (0)