Skip to content

Commit 398a0ed

Browse files
committed
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
1 parent b011783 commit 398a0ed

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

src/components/MDX/Sandpack/NavigationBar.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ export function NavigationBar({
110110
}, [isMultiFile, onContainerResize]);
111111

112112
const handleClear = () => {
113-
/**
114-
* resetAllFiles must come first, otherwise
115-
* the previous content will appear for a second
116-
* when the iframe loads.
117-
*
118-
* Plus, it should only prompt if there's any file changes
119-
*/
113+
// Only prompt when there are edits to clear.
120114
if (sandpack.editorState === 'dirty' && confirm('Clear all your edits?')) {
121115
sandpack.resetAllFiles();
116+
// Don't refresh() here. The reset files are pushed to the bundler by
117+
// the file watcher, which recompiles the preview with the original
118+
// code. Refreshing synchronously would reboot the iframe while the
119+
// client still holds the edited files: the reset compile is lost in
120+
// the reboot, so the preview keeps running the edited code (#8657).
121+
return;
122122
}
123123
refresh();
124124
};

0 commit comments

Comments
 (0)