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.
This commit is contained in:
Alex Gleason committed 2026-04-17 17:40:34 -05:00
1 parent 565ae93aed
commit 6da4b63a85
2 files changed
+18 -6

No files matched your search

+15 -3
View File
@@ -91,6 +91,12 @@ export function PreviewPane({ projectId, activeTab, onToggleView, isPreviewable
const [isFullscreen, setIsFullscreen] = useState(false);
const [deviceMode, setDeviceMode] = useState<DeviceMode>('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<HTMLDivElement>(null);
const iframeRef = useRef<HTMLIFrameElement>(null);
const iframePanelRef = useRef<ImperativePanelHandle>(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
)}
>
<iframe
key={projectId}
key={`${projectId}:${iframeReloadKey}`}
ref={iframeRef}
src={`${previewOrigin}/`}
className="w-full h-full border-0"
+3 -3
View File
@@ -142,7 +142,7 @@ export function getPreviewInjectedScript(): string {
try {
window.parent.postMessage({
jsonrpc: '2.0',
id: ++rpcId,
id: rpcId(),
method: 'updateNavigationState',
params: {
currentUrl: semanticPath,
@@ -259,7 +259,7 @@ export function getPreviewInjectedScript(): string {
try {
window.parent.postMessage({
jsonrpc: '2.0',
id: ++rpcId,
id: rpcId(),
method: 'console',
params: {
level: 'error',
@@ -279,7 +279,7 @@ export function getPreviewInjectedScript(): string {
try {
window.parent.postMessage({
jsonrpc: '2.0',
id: ++rpcId,
id: rpcId(),
method: 'console',
params: {
level: 'error',