From 4d15ce438876df3d054efbe6bc32dc1e1895eda7 Mon Sep 17 00:00:00 2001 From: Mark McKeen Date: Wed, 8 Apr 2026 15:24:12 -0600 Subject: [PATCH] Fix operator terminal SSR crash --- app/operator-terminal/page.tsx | 90 +++++++++++++++++++++------------- 1 file changed, 56 insertions(+), 34 deletions(-) diff --git a/app/operator-terminal/page.tsx b/app/operator-terminal/page.tsx index b1f587f..87d810a 100644 --- a/app/operator-terminal/page.tsx +++ b/app/operator-terminal/page.tsx @@ -3,8 +3,6 @@ import Link from 'next/link' import { useSearchParams } from 'next/navigation' import { useCallback, useEffect, useMemo, useRef, useState } from 'react' -import { Terminal } from 'xterm' -import { FitAddon } from 'xterm-addon-fit' import 'xterm/css/xterm.css' interface ReadinessPodSummary { @@ -47,6 +45,11 @@ interface LiveTerminalSession { websocketUrl: string } +type XTermModule = typeof import('xterm') +type FitAddonModule = typeof import('xterm-addon-fit') +type XTermInstance = InstanceType +type FitAddonInstance = InstanceType + type LoadState = 'idle' | 'loading' | 'ready' | 'error' const defaultAlias = (sandboxId: string | null) => `ssh openshell-${sandboxId || 'my-assistant'}` @@ -66,8 +69,8 @@ export default function OperatorTerminalPage() { const [liveSession, setLiveSession] = useState(null) const terminalContainerRef = useRef(null) - const terminalRef = useRef(null) - const fitAddonRef = useRef(null) + const terminalRef = useRef(null) + const fitAddonRef = useRef(null) const socketRef = useRef(null) const refreshReadiness = useCallback(async () => { @@ -148,43 +151,62 @@ export default function OperatorTerminalPage() { return } - const term = new Terminal({ - cursorBlink: true, - fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace', - fontSize: 13, - theme: { - background: '#000000', - foreground: '#f5f5f5', - cursor: '#76b900', - }, - scrollback: 5000, - allowTransparency: false, - }) - const fitAddon = new FitAddon() - term.loadAddon(fitAddon) - term.open(terminalContainerRef.current) - fitAddon.fit() + let disposed = false + let cleanupResize: (() => void) | null = null - terminalRef.current = term - fitAddonRef.current = fitAddon + ;(async () => { + const [{ Terminal }, { FitAddon }] = await Promise.all([ + import('xterm'), + import('xterm-addon-fit'), + ]) - const handleResize = () => { - fitAddon.fit() - if (socketRef.current?.readyState === WebSocket.OPEN) { - socketRef.current.send(JSON.stringify({ - type: 'resize', - cols: term.cols, - rows: term.rows, - })) + if (disposed || !terminalContainerRef.current) { + return } - } - window.addEventListener('resize', handleResize) + const term = new Terminal({ + cursorBlink: true, + fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace', + fontSize: 13, + theme: { + background: '#000000', + foreground: '#f5f5f5', + cursor: '#76b900', + }, + scrollback: 5000, + allowTransparency: false, + }) + const fitAddon = new FitAddon() + term.loadAddon(fitAddon) + term.open(terminalContainerRef.current) + fitAddon.fit() + + terminalRef.current = term + fitAddonRef.current = fitAddon + + const handleResize = () => { + fitAddon.fit() + if (socketRef.current?.readyState === WebSocket.OPEN) { + socketRef.current.send(JSON.stringify({ + type: 'resize', + cols: term.cols, + rows: term.rows, + })) + } + } + + window.addEventListener('resize', handleResize) + cleanupResize = () => window.removeEventListener('resize', handleResize) + })().catch((error) => { + setTerminalState('error') + setTerminalStatus(error instanceof Error ? error.message : 'Failed to load terminal renderer.') + }) return () => { - window.removeEventListener('resize', handleResize) + disposed = true + cleanupResize?.() socketRef.current?.close() - term.dispose() + terminalRef.current?.dispose() terminalRef.current = null fitAddonRef.current = null }