From 398a0edaea26da49ed6f7e5adca4fa8d5c6d56d2 Mon Sep 17 00:00:00 2001 From: Dextheking1 Date: Sun, 20 Sep 2026 22:19:28 +0200 Subject: [PATCH] fix(sandpack): make Clear button actually reset the preview When the Clear button reset edited files, it also called refresh() synchronously. The refresh reboots the preview iframe while the Sandpack client still holds the edited files, and the debounced file sync carrying the reset files is lost in the reboot. The preview then keeps running the edited code. Only refresh when there was nothing to reset; after a confirmed reset, let the file watcher push the original files so the bundler recompiles the preview with them. Fixes #8657 --- src/components/MDX/Sandpack/NavigationBar.tsx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/components/MDX/Sandpack/NavigationBar.tsx b/src/components/MDX/Sandpack/NavigationBar.tsx index 042f4b93a0e..418cfa979c7 100644 --- a/src/components/MDX/Sandpack/NavigationBar.tsx +++ b/src/components/MDX/Sandpack/NavigationBar.tsx @@ -110,15 +110,15 @@ export function NavigationBar({ }, [isMultiFile, onContainerResize]); const handleClear = () => { - /** - * resetAllFiles must come first, otherwise - * the previous content will appear for a second - * when the iframe loads. - * - * Plus, it should only prompt if there's any file changes - */ + // Only prompt when there are edits to clear. if (sandpack.editorState === 'dirty' && confirm('Clear all your edits?')) { sandpack.resetAllFiles(); + // Don't refresh() here. The reset files are pushed to the bundler by + // the file watcher, which recompiles the preview with the original + // code. Refreshing synchronously would reboot the iframe while the + // client still holds the edited files: the reset compile is lost in + // the reboot, so the preview keeps running the edited code (#8657). + return; } refresh(); };