Skip to content
AnswerWell geo Documentation

Research · 6 min read · Answerwell

Event and EventTarget Spec Compliance: isTrusted, MessagePort, and Listener Order

How Cloudflare Workers aligns Event, MessageEvent, MessagePort, and handler order with browser behavior—and what to verify in your code.

This page summarizes Cloudflare Workers documentation on Event and EventTarget behavior in the Workers runtime: fixes that bring user-constructed events, event handler attributes, and MessagePort startup in line with browsers. Those updates sit alongside broader runtime changes described in the Workers platform changelog and the dedicated post Event and EventTarget spec compliance improvements.

Changelog vs compatibility dates: what actually changes your Worker

Section titled “Changelog vs compatibility dates: what actually changes your Worker”

Runtime fixes in the Workers changelog are not something you toggle in the dashboard. Cloudflare distinguishes automatic runtime rollouts from opt-in behavior:

This changelog details meaningful changes made to Workers across the Cloudflare dashboard, Wrangler, the API, and the workerd runtime. These changes are not configurable.

This is different from compatibility dates and compatibility flags, which let you explicitly opt-in to or opt-out of specific changes to the Workers Runtime.

— Cloudflare, Workers platform changelog

Event spec compliance items in this cluster are runtime corrections: once deployed on Cloudflare’s side, your Worker sees browser-aligned behavior without a separate flag. If you need to pin older semantics, that is the domain of compatibility dates and flags (not repeated here).

MessageChannel and MessagePort: when this topic matters

Section titled “MessageChannel and MessagePort: when this topic matters”

People usually land here when porting browser code that uses postMessage, MessageChannel, or MessagePort, or when tests fail on isTrusted, handler order, or a port that never receives messages.

The Workers platform changelog records that cross-context messaging APIs became available in the runtime:

The MessageChannel and MessagePort APIs are now available in Workers.

— Cloudflare, Workers platform changelog

Earlier EventTarget work (for example CustomEvent and new EventTarget()) appears in the historic changelog. The fixes below assume you are already using standard DOM-style events and ports; they address remaining gaps versus browser specs.

isTrusted on user-constructed events: how to tell real dispatch from tests

Section titled “isTrusted on user-constructed events: how to tell real dispatch from tests”

Libraries and security checks often read event.isTrusted. In browsers, events created with new MessageEvent(...) and similar constructors are not trusted. Workers previously reported isTrusted: true for those objects, which could make unit tests pass while hiding incorrect assumptions.

Cloudflare’s correction:

User-constructed event subclasses (new MessageEvent(...), new CustomEvent(...), new ErrorEvent(...), new CloseEvent(...)) now correctly report isTrusted as false. Previously, these incorrectly reported isTrusted as true.

— Cloudflare, Event and EventTarget spec compliance improvements

The same wording appears in the Workers product changelog feed and related developer platform changelog entries.

Practical impact: code that branches on if (event.isTrusted) for synthetic events must expect false. Only events dispatched by the platform (for example a real message on a port) should be treated as trusted, matching browser rules.

onmessage vs addEventListener: listener order and registration

Section titled “onmessage vs addEventListener: listener order and registration”

Handler order people expect in the browser

Section titled “Handler order people expect in the browser”

In HTML, an onmessage property and addEventListener("message", ...) participate in one ordered listener list. Workers used to always run on<type> handlers before any addEventListener listeners, regardless of when each was registered.

Corrected behavior:

Event handler attributes such as onmessage and onabort now fire in their registration order relative to addEventListener() calls, matching browser behavior. Previously, on<type> handlers always fired before any addEventListener listeners regardless of when they were assigned.

— Cloudflare, Event and EventTarget spec compliance improvements

Setup pattern Old Workers behavior Current behavior
Set onmessage, then addEventListener("message") onmessage always first Order matches registration
addEventListener first, then assign onmessage onmessage still ran before earlier listeners Browser-style interleaving

When to care: shared utilities that mix property handlers and addEventListener, or tests that assert callback order. Prefer one style (addEventListener only) if you want deterministic cross-environment code without relying on registration timing.

MessageEvent, CloseEvent, and ErrorEvent constructors

Section titled “MessageEvent, CloseEvent, and ErrorEvent constructors”

MessageEvent: optional init and full MessageEventInit

Section titled “MessageEvent: optional init and full MessageEventInit”

Workers now accept the same constructor shapes as the spec:

new MessageEvent(type) now works without providing an init object — data defaults to null per spec. The MessageEventInit dictionary also now supports the full spec surface: origin, lastEventId, source, and ports, in addition to data.

— Cloudflare, Event and EventTarget spec compliance improvements

You can mirror browser snippets that only pass the type, or populate full metadata when forwarding or mocking channel messages.

CloseEvent and ErrorEvent: EventInit members

Section titled “CloseEvent and ErrorEvent: EventInit members”

Init dictionaries for close and error events accept standard bubbling flags:

The CloseEventInit and ErrorEventInit dictionaries now accept the standard EventInit members bubbles, cancelable, and composed.

— Cloudflare, Event and EventTarget spec compliance improvements

That matters for code that dispatches synthetic CloseEvent or ErrorEvent on custom EventTarget instances and expects composed or cancelation semantics to match MDN and HTML tests.

MessagePort: starting the port with addEventListener

Section titled “MessagePort: starting the port with addEventListener”

In browsers, attaching a message listener starts message delivery on a MessagePort. Workers only started the port when onmessage was assigned; addEventListener("message", ...) alone left the port idle.

Fixed behavior:

Adding a message event listener to a MessagePort via addEventListener("message", ...) now starts the port, matching browser behavior. Previously, only assigning onmessage would start the port.

— Cloudflare, Event and EventTarget spec compliance improvements

Common mistake: refactoring from port.onmessage = fn to port.addEventListener("message", fn) without changing anything else used to break Workers-only deployments; after the fix, both patterns should start the port. Still verify with integration tests, especially when ports are transferred via postMessage.

Section titled “Related runtime context (outside this event fix set)”

The cluster includes broader Workers platform changelog history (V8 upgrades, Streams, Durable Objects errors, and more). None of that replaces the event fixes above, but two items often appear in the same debugging session:

  • Internal errors with reference IDs — when Workers or Durable Objects throw certain internal failures, the message may include a support reference (see Workers platform changelog and Durable Objects release notes).
  • Known issues — Cloudflare maintains a known issues list for Workers; consult it if behavior still diverges after you confirm runtime version and deployment.

For pre-2023 behavior (initial EventTarget, streams flags, and early DOM APIs), see the historic Workers changelog.

  1. Treating synthetic events as trusted — assert isTrusted === false for new MessageEvent(...) in tests.
  2. Assuming onmessage runs first — re-test any code that depended on Workers’ old ordering.
  3. MessagePort only wired with addEventListener — confirm messages flow; this path now matches browsers.
  4. Confusing changelog with compat flags — event compliance here is not gated behind a named flag in these sources; incompatible assumptions are in application code, not a missing toggle.

Track ongoing runtime announcements through the Workers changelog index or the monthly changelog when you need dates alongside other Cloudflare developer platform changes.

  1. Deploy or run your Worker in preview, then exercise code paths that use MessageChannel, MessagePort, or synthetic MessageEvent / CloseEvent / ErrorEvent objects.

  2. Log or assert event.isTrusted for constructor-created events and for events delivered on a live port; compare with behavior documented in Event and EventTarget spec compliance improvements.

  3. Register addEventListener("message", ...) without setting onmessage on a MessagePort and confirm messages are received, as described in the same post.

  4. If something still looks wrong, cross-check Workers known issues and recent entries in the Workers platform changelog.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact sentence you are asking about, and we will correct or clarify it.