Skip to content

fix(web-worker): MessagePort objects passed to Worker.postMessage are available as event.ports in the worker thread#9078

Merged
AriPerkkio merged 2 commits intovitest-dev:mainfrom
whitphx:web-worker-pass-message-port
Nov 24, 2025
Merged

fix(web-worker): MessagePort objects passed to Worker.postMessage are available as event.ports in the worker thread#9078
AriPerkkio merged 2 commits intovitest-dev:mainfrom
whitphx:web-worker-pass-message-port

Conversation

@whitphx
Copy link
Copy Markdown
Contributor

@whitphx whitphx commented Nov 22, 2025

Description

MessagePort objects can be transferred to another process such as a worker and can be accessed via events.ports as below, but it's not working now. This PR fixes it.

// Main thread
const worker = new MyWorker()
const channel = new MessageChannel()
worker.postMessage('some-data', [channel.port2]) // <- `channel.port2` is of type `MessagePort` and transferred to the worker.
// Worker thread
self.onmessage = (e) => {
  const port = e.ports[0] // <- The transferred `MessagePort` object should be accessed as `e.ports[i]`, however, it's `undefined` now.
  if (port) {
    port.postMessage("data from worker")
  }
}

Refs:

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants