Skip to content

Commit f65fd57

Browse files
committed
fix: improve WebSocket error handling in CreateWorkspacePageExperimental
Refactor WebSocket error handling to ensure that errors are only set when the current socket matches the active one. This prevents unnecessary error messages when the WebSocket connection closes unexpectedly
1 parent d9ef6ed commit f65fd57

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)