fix(sandpack): make the Clear button actually reset the preview - #8668
Open
Dextheking1 wants to merge 1 commit into
Open
Dextheking1 wants to merge 1 commit into
Dextheking1 wants to merge 1 commit into
Conversation
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 reactjs#8657
Size changesDetails📦 Next.js Bundle Analysis for react-devThis analysis was generated by the Next.js Bundle Analysis action. 🤖 🎉 Global Bundle Size Decreased
DetailsThe global bundle is the javascript bundle that loads alongside every page. It is in its own category because its impact is much higher - an increase to its size means that every page on your website loads slower, and a decrease means every page loads faster. Any third party scripts you have added directly to your app using the If you want further insight into what is behind the changes, give @next/bundle-analyzer a try! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #8657.
After editing the code in an interactive sandbox and pressing Clear, the editor reverted to the original sample code but the preview kept running the edited code (e.g. the red dot kept following the cursor).
Root cause
handleClearinNavigationBar.tsxcalledsandpack.resetAllFiles()and thenrefresh()synchronously in the same tick. The refresh reboots the preview iframe while the Sandpack client still holds the edited files; the reset files only reach the client through the debounced file watcher (recompileDelay, 200ms), and that "compile" message is lost while the iframe is rebooting (IFrameProtocol.dispatchdoes not queue). When the rebooted bundler initializes, the client recompiles with its stale edited files, so the reset never takes effect in the preview.Fix
After a confirmed reset, don't call
refresh()— let the file watcher push the original files so the bundler recompiles the preview with them (the standard Sandpack reset flow).refresh()is still called when there was nothing to reset, preserving the Reload behavior.Test plan
prettier --checkandeslinton the changed file: clean.tsc --noEmit: no new errors (the 48 reported errors are pre-existing on the base commit, caused by the local install resolving different@types/reactversions than the yarn lockfile; identical count before and after this change, none in the touched file).resetAllFiles-> file watcher ->updateSandbox) delivers the original files to the running bundler without an iframe reboot.Built with a small AI agent loop under my direction; I wrote and reviewed the code and accept responsibility for it. — Benjamin (@Dextheking1)