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

Do not use callback if forward is called for PreserveOnRefresh target (#19817) (CP: 24.4) #19844

Merged
merged 2 commits into from
Aug 28, 2024
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 @@ -69,6 +69,7 @@
import com.vaadin.flow.server.Constants;
import com.vaadin.flow.server.HttpStatusCode;
import com.vaadin.flow.server.Mode;
import com.vaadin.flow.server.VaadinContext;
import com.vaadin.flow.server.VaadinSession;

/**
Expand Down Expand Up @@ -726,10 +727,21 @@ 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().getContext(),
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");
});
}
}