Skip to content

Commit

Permalink
Do not use callback if forward is called for PreserveOnRefresh target (
Browse files Browse the repository at this point in the history
  • Loading branch information
tepi authored and vaadin-bot committed Aug 28, 2024
1 parent 2151aa7 commit 5baa77e
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -726,10 +726,20 @@ private int forwardToExternalUrl(NavigationEvent event,
private int forward(NavigationEvent event, BeforeEvent beforeNavigation) {
NavigationHandler handler = beforeNavigation.getForwardTarget();

Class<? extends Component> forwardTargetType = beforeNavigation
.getForwardTargetType();

List<Class<? extends RouterLayout>> parentLayouts = RouteUtil
.getParentLayouts(event.getUI().getRouter().getRegistry(),
forwardTargetType, beforeNavigation.getForwardUrl());

boolean preserveOnRefreshTarget = isPreserveOnRefreshTarget(
forwardTargetType, parentLayouts);

NavigationEvent newNavigationEvent = getNavigationEvent(event,
beforeNavigation);
newNavigationEvent.getUI().getPage().getHistory().replaceState(null,
newNavigationEvent.getLocation(), true);
newNavigationEvent.getLocation(), !preserveOnRefreshTarget);

return handler.handle(newNavigationEvent);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.uitest.ui;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.Location;
import com.vaadin.flow.router.PreserveOnRefresh;
import com.vaadin.flow.router.QueryParameters;
import com.vaadin.flow.router.Route;

@Route("com.vaadin.flow.uitest.ui.PreserveOnRefreshForwardToView")
@PreserveOnRefresh
public class PreserveOnRefreshForwardToView extends Div
implements BeforeEnterObserver {

public PreserveOnRefreshForwardToView() {
}

@Override
public void beforeEnter(BeforeEnterEvent event) {
if (event.getLocation().getPathWithQueryParameters()
.contains("initial")) {
QueryParameters queryParameters = QueryParameters.of("afterforward",
"true");
Location location = new Location(event.getLocation().getPath(),
queryParameters);
event.getUI().getPage().getHistory().replaceState(null, location);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.uitest.ui;

import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.Route;

@Route("com.vaadin.flow.uitest.ui.PreserveOnRefreshForwardingView")
public class PreserveOnRefreshForwardingView extends Div
implements BeforeEnterObserver {

@Override
public void beforeEnter(BeforeEnterEvent event) {
event.forwardTo(PreserveOnRefreshForwardToView.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.uitest.ui;

import org.junit.Test;

import com.vaadin.flow.testutil.ChromeBrowserTest;

public class PreserveOnRefreshForwardingIT extends ChromeBrowserTest {

@Test
public void forwadingToPreserveOnRefreshRoute_allowsUpdatingQueryParameters() {
open("initial");

waitUntil(driver -> {
String url = driver.getCurrentUrl();
return url.endsWith(
"PreserveOnRefreshForwardToView?afterforward=true");
});
}
}

0 comments on commit 5baa77e

Please sign in to comment.