Skip to content

Commit e718c3a

Browse files
authored
fix: improve WebSocket error handling in CreateWorkspacePageExperimental (#17647)
Refactor WebSocket error handling to ensure that errors are only set when the current socket ref matches the active one. This prevents unnecessary error messages when the WebSocket connection closes unexpectedly This solves the problem of showing error messages because of React Strict mode rendering the page twice and opening 2 websocket connections.
1 parent a226a75 commit e718c3a

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

site/src/pages/CreateWorkspacePage/CreateWorkspacePageExperimental.tsx

+12-11
Original file line numberDiff line numberDiff line change
@@ -95,26 +95,27 @@ const CreateWorkspacePageExperimental: FC = () => {
9595

9696
// Initialize the WebSocket connection when there is a valid template version ID
9797
useEffect(() => {
98-
if (!realizedVersionId) {
99-
return;
100-
}
98+
if (!realizedVersionId) return;
10199

102100
const socket = API.templateVersionDynamicParameters(
103101
owner.id,
104102
realizedVersionId,
105103
{
106104
onMessage,
107105
onError: (error) => {
108-
setWsError(error);
106+
if (ws.current === socket) {
107+
setWsError(error);
108+
}
109109
},
110110
onClose: () => {
111-
// There is no reason for the websocket to close while a user is on the page
112-
setWsError(
113-
new DetailedError(
114-
"Websocket connection for dynamic parameters unexpectedly closed.",
115-
"Refresh the page to reset the form.",
116-
),
117-
);
111+
if (ws.current === socket) {
112+
setWsError(
113+
new DetailedError(
114+
"Websocket connection for dynamic parameters unexpectedly closed.",
115+
"Refresh the page to reset the form.",
116+
),
117+
);
118+
}
118119
},
119120
},
120121
);

0 commit comments

Comments
 (0)