Skip to content

fix(wxt): tolerate the content script reload race in MV3 dev - #2610

Closed
bompus wants to merge 3 commits into
wxt-dev:mainfrom
bompus:fix/content-script-reload-race
Closed

bompus wants to merge 3 commits into
wxt-dev:mainfrom
bompus:fix/content-script-reload-race

Conversation

@bompus

@bompus bompus commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Overview

In MV3 dev, reloadManifestContentScriptMv3 registers a content script with a check-then-act sequence: getRegisteredContentScripts(), then registerContentScripts() if the id is absent. When two wxt:reload-content-script events for the same entrypoint arrive close together, both read an empty registration and both register; the loser throws Duplicate script ID 'wxt:content-scripts/<name>.js'.

Nothing catches it. reloadContentScript calls void 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 reloadManifestContentScriptMv3 and 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:

  • Falling back to updateContentScripts throws Script with ID '…' does not exist or is not fully registered, because the winner's registration is still in flight. That rejection aborts before reloadTabsForContentScript, and the script is left unregistered.
  • Swallowing the error and continuing to reloadTabsForContentScript reloads 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 dev where 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 [email protected] in a project whose Playwright suite records service-worker unhandled rejections and fails any test that sees one. Before the change, Duplicate script ID fired roughly twice per 23-spec run. After it, three consecutive runs recorded no Duplicate script ID, no not 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

@bompus
bompus requested a review from aklinker1 as a code owner September 4, 2026 09:07
@netlify

netlify Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit 0031959
🔍 Latest deploy log https://app.netlify.com/projects/creative-fairy-df92c4/deploys/6ac2075807c082000837eaf7
😎 Deploy Preview https://deploy-preview-2610--creative-fairy-df92c4.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions Bot added the pkg/wxt Includes changes to the `packages/wxt` directory label Sep 4, 2026
@nishu-murmu
nishu-murmu enabled auto-merge (squash) October 3, 2026 04:44
@coderabbitai

coderabbitai Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4e36ec3f-6948-49a5-8524-e222f501be37

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Oct 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.65%. Comparing base (6ee6031) to head (0031959).

Files with missing lines Patch % Lines
...es/wxt/src/virtual/utils/reload-content-scripts.ts 0.00% 5 Missing and 2 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2610   +/-   ##
=======================================
  Coverage   79.64%   79.65%           
=======================================
  Files         138      138           
  Lines        4113     4118    +5     
  Branches      943      945    +2     
=======================================
+ Hits         3276     3280    +4     
+ Misses        740      739    -1     
- Partials       97       99    +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Oct 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

@wxt-dev/analytics

npm i https://pkg.pr.new/@wxt-dev/analytics@2610

@wxt-dev/auto-icons

npm i https://pkg.pr.new/@wxt-dev/auto-icons@2610

@wxt-dev/browser

npm i https://pkg.pr.new/@wxt-dev/browser@2610

@wxt-dev/i18n

npm i https://pkg.pr.new/@wxt-dev/i18n@2610

@wxt-dev/is-background

npm i https://pkg.pr.new/@wxt-dev/is-background@2610

@wxt-dev/module-react

npm i https://pkg.pr.new/@wxt-dev/module-react@2610

@wxt-dev/module-solid

npm i https://pkg.pr.new/@wxt-dev/module-solid@2610

@wxt-dev/module-svelte

npm i https://pkg.pr.new/@wxt-dev/module-svelte@2610

@wxt-dev/module-vue

npm i https://pkg.pr.new/@wxt-dev/module-vue@2610

@wxt-dev/runner

npm i https://pkg.pr.new/@wxt-dev/runner@2610

@wxt-dev/storage

npm i https://pkg.pr.new/@wxt-dev/storage@2610

@wxt-dev/unocss

npm i https://pkg.pr.new/@wxt-dev/unocss@2610

@wxt-dev/webextension-polyfill

npm i https://pkg.pr.new/@wxt-dev/webextension-polyfill@2610

wxt

npm i https://pkg.pr.new/wxt@2610

commit: 0031959

@bompus bompus closed this by deleting the head repository Oct 4, 2026
@PatrykKuniczak

Copy link
Copy Markdown
Collaborator

@bompus Closed by mistake?

@bompus

bompus commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

Yes, sorry. I deleted my fork by accident, which closed this PR. I've reopened it as #2652 with the same change, rebased onto current main.

@PatrykKuniczak

Copy link
Copy Markdown
Collaborator

@bompus No problem, i'll take a look on that tomorrow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg/wxt Includes changes to the `packages/wxt` directory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate script ID: content script reload races itself in MV3 dev, rejects unhandled

3 participants