Skip to content

Commit 17f9991

Browse files
authored
fix: reconnect terminal on non-modified key presses (#9686)
* Listen to web terminal keydown on capture Instead of bubbling. I think maybe what happens here is that xterm is capturing key presses and preventing the event from bubbling? So setting the listener on the capture phase instead works around this. Probably would also work to dipsose the terminal. * Prevent issuing terminal reload when already reloading I am not sure this actually causes any issues, but might as well. * Ignore modifier keys for reconnecting terminal
1 parent 2caf7a7 commit 17f9991

File tree

1 file changed

+34
-7
lines changed

1 file changed

+34
-7
lines changed

site/src/pages/TerminalPage/TerminalPage.tsx

+34-7
Original file line numberDiff line numberDiff line change
@@ -470,21 +470,48 @@ const useReloading = (isDisconnected: boolean) => {
470470

471471
// Retry connection on key press when it is disconnected
472472
useEffect(() => {
473-
if (!isDisconnected) {
473+
if (!isDisconnected || status === "reloading") {
474474
return;
475475
}
476476

477-
const keyDownHandler = () => {
478-
setStatus("reloading");
479-
window.location.reload();
477+
// Modifier keys should not trigger a reload.
478+
const ignoredKeys = [
479+
"Alt",
480+
"AltGraph",
481+
"CapsLock",
482+
"Control",
483+
"Fn",
484+
"FnLock",
485+
"Meta",
486+
"NumLock",
487+
"ScrollLock",
488+
"Shift",
489+
"Symbol",
490+
"SymbolLock",
491+
];
492+
493+
const keyDownHandler = (event: KeyboardEvent) => {
494+
// In addition to ignored keys, avoid reloading while modifiers are held
495+
// to cover cases where the terminal unexpectedly tries to reconnect like
496+
// when pressing ctrl+w, ctrl+r, and so on.
497+
if (
498+
!ignoredKeys.includes(event.key) &&
499+
!event.altKey &&
500+
!event.ctrlKey &&
501+
!event.metaKey &&
502+
!event.shiftKey
503+
) {
504+
setStatus("reloading");
505+
window.location.reload();
506+
}
480507
};
481508

482-
document.addEventListener("keydown", keyDownHandler);
509+
document.addEventListener("keydown", keyDownHandler, true);
483510

484511
return () => {
485-
document.removeEventListener("keydown", keyDownHandler);
512+
document.removeEventListener("keydown", keyDownHandler, true);
486513
};
487-
}, [isDisconnected]);
514+
}, [status, isDisconnected]);
488515

489516
return {
490517
status,

0 commit comments

Comments
 (0)