Skip to content

Commit 4074f5a

Browse files
authored
fix: ensure Flow client container is pre-attached on navigation (#12228)
Fixes #12080 The client-side Vaadin Router does not attach components until the `onBeforeEnter` callback is run. However, when the Flow client initializes a server-side view, it assumes its container and the UI root are connected to the document. This concerns constructor timing as well as `AttachEvent` listeners. This fixes the issue by pre-attaching the hidden container element created in the `Flow.ts` (the adapter for the client-side router) to the body.
1 parent 2165d2c commit 4074f5a

4 files changed

Lines changed: 122 additions & 9 deletions

File tree

‎flow-client/src/main/frontend/Flow.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,19 @@ export class Flow {
275275
const tag = `flow-container-${appId.toLowerCase()}`;
276276
this.container = flowRoot.$[appId] = document.createElement(tag);
277277
this.container.id = appId;
278-
279-
// It might be that components created from server expect that their content has been rendered.
280-
// Appending eagerly the container we avoid these kind of errors.
281-
// Note that the client router will move this container to the outlet if the navigation succeed
282-
this.container.style.display = 'none';
283-
document.body.appendChild(this.container);
284278
}
285279

286280
// hide flow progress indicator
287281
this.loadingFinished();
288282
}
283+
284+
// It might be that components created from server expect that their content has been rendered.
285+
// Appending eagerly the container we avoid these kind of errors.
286+
// Note that the client router will move this container to the outlet if the navigation succeed
287+
if (this.container && !this.container.isConnected) {
288+
this.container.style.display = 'none';
289+
document.body.appendChild(this.container);
290+
}
289291
return this.response!;
290292
}
291293

‎flow-client/src/test/frontend/FlowTests.ts‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ const { assert } = intern.getPlugin('chai');
55
const { sinon } = intern.getPlugin('sinon') as { sinon: SinonStatic };
66

77
// API to test
8-
import { Flow, NavigationParameters } from '../../main/frontend/Flow';
8+
import {
9+
Flow,
10+
NavigationParameters,
11+
PreventAndRedirectCommands
12+
} from '../../main/frontend/Flow';
913
import { ConnectionState, ConnectionStateStore } from '@vaadin/common-frontend';
1014
// Intern does not serve webpack chunks, adding deps here in order to
1115
// produce one chunk, because dynamic imports in Flow.ts will not work.
@@ -763,6 +767,43 @@ suite('Flow', () => {
763767
$wnd.dispatchEvent(new Event('online')); // caught by DefaultConnectionStateHandler
764768
assert.equal($wnd.Vaadin.connectionState.state, ConnectionState.RECONNECTING);
765769
});
770+
771+
test("should pre-attach container element on every navigation", async () => {
772+
stubServerRemoteFunction('foobar-12345');
773+
mockInitResponse('foobar-12345');
774+
775+
const flow = new Flow();
776+
const route = flow.serverSideRoutes[0];
777+
778+
const flowRouteParams = {pathname: 'Foo', search: ''};
779+
const otherRouteParams = {pathname: 'Lorem', search: ''};
780+
781+
// Initial navigation
782+
const container = await route.action(flowRouteParams);
783+
assert.isTrue(container.isConnected);
784+
assert.equal(container.style.display, 'none');
785+
786+
// @ts-ignore
787+
await container.onBeforeEnter(flowRouteParams, {});
788+
assert.isTrue(container.isConnected);
789+
assert.equal(container.style.display, '');
790+
791+
// Leave
792+
793+
// @ts-ignore
794+
await container.onBeforeLeave(otherRouteParams, {});
795+
// The router detaches the container, possibly because it renders a client-side view
796+
container.parentNode!.removeChild(container);
797+
798+
await route.action(flowRouteParams);
799+
assert.isTrue(container.isConnected);
800+
assert.equal(container.style.display, 'none');
801+
802+
// @ts-ignore
803+
await container.onBeforeEnter(flowRouteParams, {});
804+
assert.isTrue(container.isConnected);
805+
assert.equal(container.style.display, '');
806+
});
766807
});
767808

768809
function stubServerRemoteFunction(

‎flow-tests/test-ccdm-flow-navigation/src/main/java/com/vaadin/flow/navigate/HelloWorldView.java‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,43 @@
1616

1717
package com.vaadin.flow.navigate;
1818

19+
import com.vaadin.flow.component.Text;
20+
import com.vaadin.flow.component.html.NativeButton;
21+
import com.vaadin.flow.component.html.Paragraph;
1922
import com.vaadin.flow.component.html.Span;
2023
import com.vaadin.flow.router.PageTitle;
2124
import com.vaadin.flow.router.Route;
22-
import com.vaadin.flow.component.html.NativeButton;
2325

2426
@Route(value = "hello")
2527
@PageTitle("Hello World")
2628
public class HelloWorldView extends Span {
27-
2829
public static final String NAVIGATE_ABOUT = "navigate-about";
30+
public static final String IS_CONNECTED_ON_INIT = "is-connected-on-init";
31+
public static final String IS_CONNECTED_ON_ATTACH = "is-connected-on-attach";
32+
33+
private final Span isConnectedOnInit = new Span("");
34+
private final Span isConnectedOnAttach = new Span("");
2935

3036
public HelloWorldView() {
3137
setId("hello-world-view");
3238
NativeButton toAbout = new NativeButton("Say hello",
3339
e -> getUI().get().navigate("about"));
3440
toAbout.setId(NAVIGATE_ABOUT);
3541
add(toAbout);
42+
43+
isConnectedOnInit.setId(IS_CONNECTED_ON_INIT);
44+
updateIsConnected(isConnectedOnInit);
45+
add(new Paragraph(new Text("Connected on init: "), isConnectedOnInit));
46+
47+
isConnectedOnAttach.setId(IS_CONNECTED_ON_ATTACH);
48+
isConnectedOnAttach.addAttachListener(
49+
event -> updateIsConnected(isConnectedOnAttach));
50+
add(new Paragraph(new Text("Connected on attach: "),
51+
isConnectedOnAttach));
3652
}
3753

54+
private void updateIsConnected(Span output) {
55+
output.getElement()
56+
.executeJs("this.textContent=String(this.isConnected)");
57+
}
3858
}

‎flow-tests/test-ccdm-flow-navigation/src/test/java/com/vaadin/flow/navigate/NavigateBetweenViewsIT.java‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
import org.openqa.selenium.By;
2222

2323
import com.vaadin.flow.component.html.testbench.NativeButtonElement;
24+
import com.vaadin.flow.component.html.testbench.SpanElement;
2425
import com.vaadin.flow.testutil.ChromeBrowserTest;
2526

27+
import static com.vaadin.flow.navigate.HelloWorldView.IS_CONNECTED_ON_ATTACH;
28+
import static com.vaadin.flow.navigate.HelloWorldView.IS_CONNECTED_ON_INIT;
2629
import static com.vaadin.flow.navigate.HelloWorldView.NAVIGATE_ABOUT;
2730

2831
public class NavigateBetweenViewsIT extends ChromeBrowserTest {
@@ -70,8 +73,55 @@ public void openTsView_navigateToFlow_navigationSuccessful() {
7073
$(NativeButtonElement.class).id(NAVIGATE_ABOUT).isDisplayed());
7174
}
7275

76+
@Test
77+
public void openFlowView_isConnectedOnAttach() {
78+
getDriver().get(getRootURL() + "/hello");
79+
waitForDevServer();
80+
81+
Assert.assertThat(getDriver().getCurrentUrl(),
82+
CoreMatchers.endsWith("/hello"));
83+
84+
assertIsConnected();
85+
86+
// Navigate away and back
87+
$(NativeButtonElement.class).id(NAVIGATE_ABOUT).click();
88+
waitUntil(input -> $("about-view").first().$("a").id("navigate-hello")
89+
.isDisplayed());
90+
91+
getInShadowRoot(findElement(By.tagName("about-view")),
92+
By.id("navigate-hello")).click();
93+
94+
assertIsConnected();
95+
}
96+
97+
@Test
98+
public void openTsView_navigateToFlowView_isConnectedOnAttach() {
99+
getDriver().get(getRootURL() + "/");
100+
waitForDevServer();
101+
102+
waitUntil(input -> $("about-view").first().$("a").id("navigate-hello")
103+
.isDisplayed());
104+
105+
getInShadowRoot(findElement(By.tagName("about-view")),
106+
By.id("navigate-hello")).click();
107+
108+
getCommandExecutor().waitForVaadin();
109+
110+
assertIsConnected();
111+
}
112+
73113
@Override
74114
protected String getRootURL() {
75115
return super.getRootURL() + "/context-path";
76116
}
117+
118+
private void assertIsConnected() {
119+
assertIsConnectedById(IS_CONNECTED_ON_INIT);
120+
assertIsConnectedById(IS_CONNECTED_ON_ATTACH);
121+
}
122+
123+
private void assertIsConnectedById(String id) {
124+
Assert.assertTrue(Boolean.parseBoolean(
125+
waitUntil(driver -> $(SpanElement.class).id(id)).getText()));
126+
}
77127
}

0 commit comments

Comments
 (0)