Skip to content

fix: keep dev worker URLs same-origin - #2617

Open
dvd233 wants to merge 2 commits into
wxt-dev:mainfrom
dvd233:codex/contrib-wxt-2599-worker-same-origin
Open

dvd233 wants to merge 2 commits into
wxt-dev:mainfrom
dvd233:codex/contrib-wxt-2599-worker-same-origin

Conversation

@dvd233

@dvd233 dvd233 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Overview

In Chromium dev mode, Vite's ?worker and ?worker&url transforms expose localhost worker URLs to extension pages. Those pages have a chrome-extension:// origin, so Chrome 148+ rejects the cross-origin dedicated worker URL and can terminate the render process.

Add a Chromium-only Vite dev plugin that replaces Vite's generated worker URL with an extension-origin Blob shim while loading the worker module from the dev server. Constructor imports revoke their one-shot Blob URL; reusable ?worker&url imports keep their URL alive and buffer early messages until the module is ready. Firefox and Safari retain their existing dev-server path because their extension Blob-worker/CSP behavior differs.

Production builds are unchanged.

Manual Testing

  • bun run --filter wxt test run src/core/builders/vite/plugins/__tests__/devServerWorkers.test.ts e2e/tests/dev.test.ts
  • bun run --filter wxt test run
  • bun run --filter wxt check
  • Tested an unpacked Chromium MV3 extension with ?worker, two reused ?worker&url workers, immediate messages, and transferred MessagePorts; all returned ready.
  • Tested the unchanged Firefox MV2 direct-worker path with the same message/port flow.

Related Issue

This PR closes #2599

@dvd233
dvd233 requested a review from aklinker1 as a code owner September 8, 2026 14:18
@netlify

netlify Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit 51dc7d7
🔍 Latest deploy log https://app.netlify.com/projects/creative-fairy-df92c4/deploys/6ace0af2ad409298311522ac
😎 Deploy Preview https://deploy-preview-2617--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 8, 2026
@nishu-murmu
nishu-murmu enabled auto-merge (squash) October 1, 2026 12:59
auto-merge was automatically disabled October 13, 2026 10:41

Head branch was pushed to by a user without write access

@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f1d68925-b65d-48e9-b219-0d87abf963df
Commits

Reviewing files that changed from the base of the PR and between f7786be and 51dc7d7.

Files selected for processing: 5
  • packages/wxt/e2e/tests/dev.test.ts
  • packages/wxt/src/internal/builders/vite/index.ts
  • packages/wxt/src/internal/builders/vite/plugins/__tests__/dev-server-workers.test.ts
  • packages/wxt/src/internal/builders/vite/plugins/dev-server-workers.ts
  • packages/wxt/src/internal/builders/vite/plugins/index.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

Adds a Vite development plugin that rewrites matching worker URLs into same-origin blob URLs for Chromium. The generated worker scripts handle constructor and URL-module requests differently. Firefox and Safari retain their worker URLs, and tests cover these browser-specific outputs.

Changes: Development worker URL handling

Layer / File(s) Summary
Register and implement worker rewriting
packages/wxt/src/internal/builders/vite/index.ts, packages/wxt/src/internal/builders/vite/plugins/dev-server-workers.ts, packages/wxt/src/internal/builders/vite/plugins/index.ts
The shared Vite configuration registers the plugin. During development, it matches worker URLs on the dev-server origin and replaces them with blob URLs. Constructor workers use a revoking blob script; URL-module workers buffer early messages and redispatch them after import.
Verify transformed worker output
packages/wxt/src/internal/builders/vite/plugins/__tests__/dev-server-workers.test.ts, packages/wxt/e2e/tests/dev.test.ts
Unit tests cover replacement behavior, idempotence, and non-matching URLs. End-to-end tests check Chromium output and verify that Firefox and Safari retain worker URLs.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant ViteBuilder
  participant ViteDevServer
  participant DevServerWorkersPlugin
  participant ChromiumExtensionPage
  participant WorkerBlobScript
  ViteBuilder->>DevServerWorkersPlugin: register plugin
  ViteDevServer->>DevServerWorkersPlugin: provide origin and worker module
  DevServerWorkersPlugin-->>ViteDevServer: return transformed module code
  ViteDevServer->>ChromiumExtensionPage: serve module with blob URL
  ChromiumExtensionPage->>WorkerBlobScript: start worker from blob URL
  WorkerBlobScript->>ViteDevServer: import original worker module
Loading
Merge Risk: Minimal · up to 51dc7

No actionable merge-blocking issue was established. The change is ready for normal checks before merge.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: keeping development worker URLs same-origin.
Description check Passed The description includes all required sections. It explains the change and rationale, lists manual tests, and identifies the related issue as #2599.
Linked Issues check Passed Directly linked issue #2599 requires worker URLs on extension pages to be same-origin in dev mode for both ?worker and ?worker&url. The reviewed head adds devServerWorkers (packages/wxt/src/inte…
Out of Scope Changes check Passed All changed files map to #2599 or its supporting verification. The plugin source implements the same-origin worker requirement. The barrel export in plugins/index.ts and the registration in builders/v…
Full details: Linked Issues check

Explanation

Directly linked issue #2599 requires worker URLs on extension pages to be same-origin in dev mode for both ?worker and ?worker&url. The reviewed head adds devServerWorkers (packages/wxt/src/internal/builders/vite/plugins/dev-server-workers.ts) and registers it in ViteBuilder.getBaseConfig. For serve in non-Firefox and non-Safari builds, replaceDevServerWorkerUrl replaces the localhost worker_file URL literal with URL.createObjectURL(new Blob(...)). A blob URL created by an extension page inherits that page's chrome-extension:// origin, so new Worker(blobUrl) is same-origin. The ?worker path (constructor import) revokes its one-shot blob at worker startup. The ?worker&url path keeps the blob alive and buffers early messages until the dynamic import() resolves. The e2e test in packages/wxt/e2e/tests/dev.test.ts checks the generated code for both query forms. Production is unchanged because the plugin applies only to serve. Limits of this assessment: static checks confirm the generated code shape and origin behavior. The cross-origin module fetch from the blob worker to the dev server depends on dev server CORS. That runtime behavior is supported only by the author's reported manual Chromium test and the issue's own workaround, not by a test in this PR.

Full details: Out of Scope Changes check

Explanation

All changed files map to #2599 or its supporting verification. The plugin source implements the same-origin worker requirement. The barrel export in plugins/index.ts and the registration in builders/vite/index.ts connect the plugin to the Vite build. Unit tests in plugins/tests/dev-server-workers.test.ts cover URL replacement, revocation behavior, and non-matching URLs. The e2e tests in e2e/tests/dev.test.ts cover the Firefox and Safari unchanged path and the Chromium blob path. The ?worker&url message buffering is an implementation choice needed so that early messages reach the worker before its module listeners attach. It supports the issue's goal of working reusable worker URLs and is not unrelated behavior. Minor note: the PR description's test command names src/core/builders/vite/plugins/__tests__/devServerWorkers.test.ts, while the changed files are under src/internal/builders/vite/plugins/__tests__/dev-server-workers.test.ts. This is a documentation inconsistency, not out-of-scope code.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 3 functions across 5 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@PatrykKuniczak

Copy link
Copy Markdown
Collaborator

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@PatrykKuniczak

PatrykKuniczak commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

@dvd233 GPT review:

The overall approach makes sense, especially handling ?worker and ?worker&url separately. However, I have a few concerns that I'd like to see addressed before merging:

1. CSP compatibility (potential blocker)

The implementation relies on URL.createObjectURL() to create Blob-based workers. Chrome MV3 has strict CSP restrictions, and blob: is not an allowed source for extension pages. This seems to conflict with the intended MV3 support.

Could you verify this against the default WXT-generated MV3 manifest and provide a reproducible test in a real unpacked Chrome extension? I'd particularly like to confirm that no CSP changes or exceptions are required.

2. Missing real browser integration tests

The current E2E tests primarily inspect the transformed worker code rather than actually running it in Chromium. They don't verify that:

  • Blob workers can be created under MV3 CSP.
  • The worker successfully loads its module from the Vite dev server.
  • Messages sent immediately after construction are delivered correctly.
  • Transferred MessagePort instances work as expected.

A real Chromium MV3 integration test would provide much stronger confidence.

3. Worker location semantics

The implementation changes self.location from the original Vite URL to a Blob URL. This may break workers or dependencies that rely on self.location for relative resource resolution (e.g. fetch('./data.json') or new URL('./asset.wasm', self.location.href)).

Please consider whether this behavior needs to be preserved or at least explicitly tested/documented.

4. Message event compatibility

Replaying buffered messages by constructing new MessageEvent instances is not fully equivalent to delivering the original events (for example, isTrusted will differ). This is probably acceptable for most use cases, but it would be good to document this limitation.

5. Transform robustness

The implementation relies on regexes matching Vite-generated worker wrappers. This is somewhat brittle across Vite versions. Please ensure the tests cover the supported Vite output formats. Also, returning map: null may negatively affect source mapping and debugging.

**My main concern is, The Blob-based approach may solve the same-origin issue while introducing a new CSP restriction in MV3. I'd consider this a blocker until the proposed solution is demonstrated to work with the default WXT CSP in a real Chromium extension.


Are you able to answer to that?
After your answers i'll be able to check it all again.
Thanks

This branch has not been deployed

No deployments
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.

Dev mode produces cross-origin worker URLs — new Worker() crashes the render process in Chrome 148+

2 participants