fix(wxt): tolerate the content script reload race in MV3 dev - #2610
Open
bompus wants to merge 1 commit into
Open
fix(wxt): tolerate the content script reload race in MV3 dev#2610bompus wants to merge 1 commit into
bompus wants to merge 1 commit into
Conversation
✅ Deploy Preview for creative-fairy-df92c4 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
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.
Overview
In MV3 dev,
reloadManifestContentScriptMv3registers a content script with a check-then-act sequence:getRegisteredContentScripts(), thenregisterContentScripts()if the id is absent. When twowxt:reload-content-scriptevents for the same entrypoint arrive close together, both read an empty registration and both register; the loser throwsDuplicate script ID 'wxt:content-scripts/<name>.js'.Nothing catches it.
reloadContentScriptcallsvoid reloadContentScriptMv3(payload), and the WebSocket listener does not await it, so the rejection lands as an unhandled promise rejection in the service worker.This PR catches the duplicate-id rejection inside
reloadManifestContentScriptMv3and returns. The winner is registering the identical script and reloads the matching tabs once its registration lands, so the loser has nothing left to do.Two alternatives that look reasonable and are not — both tried against a real dev session:
updateContentScriptsthrowsScript with ID '…' does not exist or is not fully registered, because the winner's registration is still in flight. That rejection aborts beforereloadTabsForContentScript, and the script is left unregistered.reloadTabsForContentScriptreloads the tab before the winner's registration lands, so the page comes back without the content script.The comment in the diff records both so neither gets reintroduced.
Manual Testing
The race is timing-dependent, so it does not reproduce on demand. It shows up on an MV3 project in
wxt devwhere several content script entrypoints share source, so one save emits multiple reload events in quick succession.I have been running this change as a local patch against
wxt@0.21.4in a project whose Playwright suite records service-worker unhandled rejections and fails any test that sees one. Before the change,Duplicate script IDfired roughly twice per 23-spec run. After it, three consecutive runs recorded noDuplicate script ID, nonot fully registered, and no other rejection; all 23 specs passed each time.There are no existing tests under
packages/wxt/src/virtual/, so none were added here. Happy to add one if there is a preferred harness for the virtual entrypoints.Related Issue
This PR closes #2609