From 6da4b63a8510c03a37b6a52e3cd0341252eb6133 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 17 Apr 2026 17:40:34 -0500 Subject: [PATCH] Fix preview pane getting stuck during auto-refresh on rapid builds The refresh mechanism posted a JSON-RPC `refresh` command to the nested iframe.diy frames. During rapid build cycles, the command could be silently dropped when the inner iframe was mid-reload, not yet mounted, or when the outer frame's handshake was still pending \u2014 leaving the preview stuck on a stale build with no way to recover short of reloading the whole page. Switch to remounting the iframe via a React key bump, which always works regardless of handshake state. Also fix a bug in the injected preview script where `++rpcId` (a function) silently corrupted the id generator to NaN, breaking navigation state and uncaught error reporting. --- src/components/Shakespeare/PreviewPane.tsx | 18 +++++++++++++++--- src/lib/previewInjectedScript.ts | 6 +++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/components/Shakespeare/PreviewPane.tsx b/src/components/Shakespeare/PreviewPane.tsx index 9ba57a7f..1e109690 100644 --- a/src/components/Shakespeare/PreviewPane.tsx +++ b/src/components/Shakespeare/PreviewPane.tsx @@ -91,6 +91,12 @@ export function PreviewPane({ projectId, activeTab, onToggleView, isPreviewable const [isFullscreen, setIsFullscreen] = useState(false); const [deviceMode, setDeviceMode] = useState('laptop'); const [isLogsOpen, setIsLogsOpen] = useState(false); + // Incremented to force a full remount of the preview iframe. Using a + // remount (via React key) is more reliable than sending a `refresh` + // JSON-RPC to the nested iframe.diy frames: messages can be silently + // dropped if the inner iframe is mid-reload, not yet mounted, or if + // the outer frame's handshake is still pending. A remount always works. + const [iframeReloadKey, setIframeReloadKey] = useState(0); const previewContainerRef = useRef(null); const iframeRef = useRef(null); const iframePanelRef = useRef(null); @@ -284,8 +290,14 @@ export function PreviewPane({ projectId, activeTab, onToggleView, isPreviewable }, [sendNavigationCommand]); const refreshIframe = useCallback(() => { - sendNavigationCommand('refresh'); - }, [sendNavigationCommand]); + // Force a full remount of the iframe rather than sending a `refresh` + // JSON-RPC command. The RPC path is unreliable during rapid build + // cycles because the command has to traverse two iframes (outer + // iframe.diy frame → inner app iframe) and can be silently dropped + // if the inner iframe is mid-reload, not yet mounted, or the outer + // frame's handshake is still pending. + setIframeReloadKey((n) => n + 1); + }, []); const toggleFullscreen = useCallback(() => { setIsFullscreen(prev => !prev); @@ -709,7 +721,7 @@ export function PreviewPane({ projectId, activeTab, onToggleView, isPreviewable )} >