Skip to content

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

Closed

Description

@bompus

Describe the bug

In MV3 dev, reloadManifestContentScriptMv3 registers content scripts with a check-then-act sequence. When two wxt:reload-content-script events for the same entrypoint arrive close together, both read a registration list that does not yet contain the script, and both call registerContentScripts. The loser throws Duplicate script ID 'wxt:content-scripts/<name>.js'.

Because the WebSocket listener does not await or catch, the rejection lands as an unhandled promise rejection in the service worker.

dist/virtual/background-entrypoint.mjs (0.21.4), the check-then-act:

async function reloadManifestContentScriptMv3(contentScript) {
	const id = `wxt:${contentScript.js[0]}`;
	const registered = await browser.scripting.getRegisteredContentScripts();
	const existing = registered.find((cs) => cs.id === id);
	if (existing) {
		await browser.scripting.updateContentScripts([{ ...contentScript, id, css: contentScript.css ?? [] }]);
	} else {
		await browser.scripting.registerContentScripts([{ ...contentScript, id, css: contentScript.css ?? [] }]);
	}
	await reloadTabsForContentScript(contentScript);
}

and why nothing catches it:

ws.addWxtEventListener("wxt:reload-content-script", (event) => {
	reloadContentScript(event.detail);        // not awaited, not caught
});

function reloadContentScript(payload) {
	if (browser.runtime.getManifest().manifest_version == 2) reloadContentScriptMv2(payload);
	else reloadContentScriptMv3(payload);     // promise not returned either
}

reloadContentScript does not return the promise from its MV3 branch, so a .catch() at the listener would not help — the rejection has to be handled inside reloadManifestContentScriptMv3.

Relevant background: registrations made with scripting.registerContentScripts() persist across service worker restarts (noted in #2239), so the window where two events can both observe "not yet registered" is real rather than theoretical.

Impact

Intermittent, and normally invisible because it only prints to the service worker console. It became visible for us because our Playwright suite records service-worker unhandled rejections and fails any test that sees one. Across a 23-spec run in dev mode it fired roughly twice per run, failing whichever specs were in flight.

Reproduction

Not reliably reproducible on demand — it is a race. 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. Two error entries 3 ms apart in one run is typical:

Duplicate script ID 'wxt:content-scripts/ttd.js'   2026-09-04T08:18:12.039Z
Duplicate script ID 'wxt:content-scripts/ttd.js'   2026-09-04T08:18:12.042Z

Suggested fix

Treat a lost race as the update it would have been:

	} else {
		try {
			await browser.scripting.registerContentScripts([{ ...contentScript, id, css: contentScript.css ?? [] }]);
		} catch (err) {
			if (!String(err?.message ?? err).includes("Duplicate script ID")) throw err;
			await browser.scripting.updateContentScripts([{ ...contentScript, id, css: contentScript.css ?? [] }]);
		}
	}

Having reloadContentScript return the promise from its MV3 branch would also let callers handle failures, though it would not fix this race on its own.

I am running this as a local patch against 0.21.4 and it clears the error. Happy to open a PR if the approach looks right.

Versions

  • wxt 0.21.4
  • MV3, Chromium (Microsoft Edge), Windows 11
  • Bun 1.4.0

Activity

  1. bompus commented on Sep 4, 2026

    @bompus
    ContributorAuthor

    Correction to the suggested fix above: do not fall back to updateContentScripts. I ran that version against a real dev session and it trades one error for a worse one.

    When the loser of the race calls updateContentScripts, the winner's registerContentScripts has not completed yet, so the update throws:

    Script with ID 'wxt:content-scripts/espn-beforeunload-guard.js' does not exist or is not fully registered
    

    That rejection propagates out of reloadManifestContentScriptMv3 before reloadTabsForContentScript(contentScript) runs, so the content script is left unregistered and the page it targets never gets it. In my case the extension's content script stopped attaching entirely, which was a much louder failure than the original intermittent Duplicate script ID.

    The winner is registering the identical script, so the loser should simply drop its attempt and continue:

    	} else {
    		try {
    			await browser.scripting.registerContentScripts([{ ...contentScript, id, css: contentScript.css ?? [] }]);
    		} catch (err) {
    			// The concurrent registration owns this id and is registering the same script.
    			// Do not fall back to updateContentScripts: that registration is not complete yet.
    			if (!String(err?.message ?? err).includes("Duplicate script ID")) throw err;
    		}
    	}
    	await reloadTabsForContentScript(contentScript);

    Running this version now. Happy to send it as a PR if the approach looks right.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions