Skip to content

Commit 73d9510

Browse files
authored
Fix showing "Connection Trouble" warning after fork (codesandbox#1234)
* Fixed showing "Connection Trouble" warning after fork. * Preview: moved localClose to an instance field. * Preview -> SSE socket: try websocket transport first, if not supported fall back to long polling.
1 parent 83db4c8 commit 73d9510

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"no-underscore-dangle": "off",
2828
"no-nested-ternary": "warn",
2929
"react/require-default-props": "off",
30-
"import/no-named-default": 0
30+
"import/no-named-default": 0,
31+
"no-param-reassign": ["error", { "props": false }]
3132
}
3233
}

packages/app/src/app/components/Preview/index.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ class BasePreview extends React.Component<Props, State> {
151151
};
152152
// TODO: Find typedefs for this
153153
$socket: ?any;
154+
connectTimeout: ?number;
155+
// indicates if the socket closing is initiated by us
156+
localClose: boolean;
154157

155158
constructor(props: Props) {
156159
super(props);
@@ -182,6 +185,8 @@ class BasePreview extends React.Component<Props, State> {
182185
};
183186

184187
if (this.serverPreview) {
188+
this.connectTimeout = null;
189+
this.localClose = false;
185190
this.setupSSESockets();
186191
}
187192
this.listener = listen(this.handleMessage);
@@ -204,12 +209,11 @@ class BasePreview extends React.Component<Props, State> {
204209

205210
setupSSESockets = async () => {
206211
const hasInitialized = !!this.$socket;
207-
let connectTimeout = null;
208212

209-
function onTimeout(setServerStatus) {
210-
connectTimeout = null;
211-
if (setServerStatus) {
212-
setServerStatus('disconnected');
213+
function onTimeout(comp) {
214+
comp.connectTimeout = null;
215+
if (comp.props.setServerStatus) {
216+
comp.props.setServerStatus('disconnected');
213217
}
214218
}
215219

@@ -218,27 +222,30 @@ class BasePreview extends React.Component<Props, State> {
218222
frameInitialized: false,
219223
});
220224
if (this.$socket) {
225+
this.localClose = true;
221226
this.$socket.close();
227+
// we need this setTimeout() for socket open() to work immediately after close()
222228
setTimeout(() => {
223-
if (this.$socket) {
224-
connectTimeout = setTimeout(
225-
() => onTimeout(this.props.setServerStatus),
226-
3000
227-
);
228-
this.$socket.open();
229-
}
229+
this.connectTimeout = setTimeout(() => onTimeout(this), 3000);
230+
this.$socket.open();
230231
}, 0);
231232
}
232233
} else {
233234
const socket = io(getSSEUrl(), {
234235
autoConnect: false,
236+
transports: ['websocket', 'polling'],
235237
});
236238
this.$socket = socket;
237239
if (process.env.NODE_ENV === 'development') {
238240
window.$socket = socket;
239241
}
240242

241243
socket.on('disconnect', () => {
244+
if (this.localClose) {
245+
this.localClose = false;
246+
return;
247+
}
248+
242249
if (this.props.setServerStatus) {
243250
let status = 'disconnected';
244251
if (this.state.hibernated) {
@@ -252,9 +259,9 @@ class BasePreview extends React.Component<Props, State> {
252259
});
253260

254261
socket.on('connect', async () => {
255-
if (connectTimeout) {
256-
clearTimeout(connectTimeout);
257-
connectTimeout = null;
262+
if (this.connectTimeout) {
263+
clearTimeout(this.connectTimeout);
264+
this.connectTimeout = null;
258265
}
259266

260267
if (this.props.setServerStatus) {
@@ -352,10 +359,7 @@ class BasePreview extends React.Component<Props, State> {
352359
}
353360
});
354361

355-
connectTimeout = setTimeout(
356-
() => onTimeout(this.props.setServerStatus),
357-
3000
358-
);
362+
this.connectTimeout = setTimeout(() => onTimeout(this), 3000);
359363
socket.open();
360364
}
361365
};
@@ -378,6 +382,7 @@ class BasePreview extends React.Component<Props, State> {
378382
}
379383

380384
if (this.$socket) {
385+
this.localClose = true;
381386
this.$socket.close();
382387
}
383388
}

0 commit comments

Comments
 (0)