Skip to content

Commit 45d5b3b

Browse files
vaadin-botArtur-
andauthored
fix: Make Vite messages from browser to server work (#21565) (#21567)
Fixes #21564 Co-authored-by: Artur <artur@vaadin.com>
1 parent 91d752d commit 45d5b3b

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @ts-expect-error
2+
if (import.meta.hot) {
3+
// @ts-ignore
4+
window.importmetahot = import.meta.hot;
5+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2000-2025 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.uitest.ui;
17+
18+
import com.vaadin.flow.component.AttachEvent;
19+
import com.vaadin.flow.component.UI;
20+
import com.vaadin.flow.component.dependency.JsModule;
21+
import com.vaadin.flow.component.html.Div;
22+
import com.vaadin.flow.component.html.NativeButton;
23+
import com.vaadin.flow.component.page.Page;
24+
import com.vaadin.flow.router.Route;
25+
import com.vaadin.flow.uitest.servlet.ViewTestLayout;
26+
27+
@Route(value = "com.vaadin.flow.uitest.ui.ViteCommunicationView", layout = ViewTestLayout.class)
28+
@JsModule("./vite-communication.ts")
29+
public class ViteCommunicationView extends Div {
30+
31+
@Override
32+
protected void onAttach(AttachEvent attachEvent) {
33+
super.onAttach(attachEvent);
34+
UI ui = attachEvent.getUI();
35+
Page page = ui.getPage();
36+
page.executeJs(
37+
"""
38+
window.importmetahot.on('test-event-response', (event) => {
39+
document.querySelector('#response').innerText += 'Got event test-event-response with data ' + JSON.stringify(event) + '';
40+
});
41+
""");
42+
43+
NativeButton sendMessage = new NativeButton("Send message",
44+
e -> ui.getPage().executeJs(
45+
"window.importmetahot.send('test-event', {foo: 'bar'});"));
46+
sendMessage.setId("send");
47+
Div response = new Div();
48+
response.setId("response");
49+
50+
add(sendMessage, response);
51+
52+
}
53+
54+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2000-2025 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.uitest.ui;
17+
18+
import org.junit.Test;
19+
20+
import com.vaadin.flow.component.html.testbench.DivElement;
21+
import com.vaadin.flow.component.html.testbench.NativeButtonElement;
22+
import com.vaadin.flow.testutil.ChromeBrowserTest;
23+
24+
public class ViteCommunicationIT extends ChromeBrowserTest {
25+
26+
@Test
27+
public void messageSentToViteAndBack() {
28+
open();
29+
$(NativeButtonElement.class).id("send").click();
30+
waitUntil(driver -> $(DivElement.class).id("response").getText().equals(
31+
"Got event test-event-response with data {\"foo\":\"bar\"}"));
32+
}
33+
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { UserConfigFn } from 'vite';
2+
import { overrideVaadinConfig } from './vite.generated';
3+
4+
const customConfig: UserConfigFn = (env) => ({
5+
plugins: [
6+
{
7+
name: 'communication-test',
8+
configureServer(server) {
9+
server.ws.on('test-event', (data) => {
10+
server.ws.send('test-event-response', data);
11+
});
12+
}
13+
}
14+
]
15+
});
16+
17+
export default overrideVaadinConfig(customConfig);

vaadin-dev-server/src/main/java/com/vaadin/base/devserver/viteproxy/ViteWebsocketConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public CompletionStage<?> onText(WebSocket webSocket, CharSequence data,
139139
public void send(String message)
140140
throws InterruptedException, ExecutionException {
141141
CompletableFuture<WebSocket> send = clientWebsocket.get()
142-
.sendText(message, false);
142+
.sendText(message, true);
143143
send.get();
144144
}
145145

0 commit comments

Comments
 (0)