v160 · dom · workers · interop fix

SharedWorkerConnectEventEmptyStringData

Every time a page connects to a SharedWorker, the browser fires a connect MessageEvent inside the worker. The HTML spec says that event’s data is the empty string. Chrome 159 and earlier left it as null; Chrome 160 initialises it to "", matching the spec, Firefox and Safari. The handshake itself (ports, source) is unchanged — but any worker code that branched on e.data === null, defaulted with ??, or routed on typeof e.data now takes a different path.

No flag needed: in Chrome 160 the Blink runtime feature SharedWorkerConnectEventEmptyStringData is stable (on by default). It is a kill switch only — there is no chrome://flags entry. To reproduce the old null behaviour on 160+ for testing, launch Chrome with --disable-blink-features=SharedWorkerConnectEventEmptyStringData. Every demo below runs a real SharedWorker and reads the real, browser-fired event — nothing is simulated.

concepts

  1. Connect event inspector

    Connect to a real shared worker — a brand-new one or the one already running — and read every field of the browser-fired connect event back: data and its typeof, origin, lastEventId, the frozen ports array, source === ports[0], isTrusted. A verdict tells you whether this browser follows the spec.

  2. Connection hub

    A practical presence hub: every page (and every tab) that connects is logged by the worker with the data of its own connect event. The first connection comes from “run a worker”, later ones from the SharedWorker constructor — the spec says "" for both, and the roster shows it per connection.

  3. Null-sentinel migration

    Eight handler patterns found in real worker code, executed against the real event inside the worker. e.data === null, e.data ?? default and typeof e.data === 'object' flip branch between Chrome 159 and 160; !e.data and e.type === 'connect' don’t. See what your browser does and what to migrate to.

  4. Synthetic connect fidelity

    For test harnesses that fake connect events: build candidates with new MessageEvent('connect', init) inside the worker and diff them field by field against the real one. The constructor’s default data is still null — only the browser-fired event changed — so a fake must pass data: "" to match Chrome 160.

why it shipped

The spec has always said the connect event’s data is the empty string. Chromium created that event through a special-purpose MessageEvent constructor that never set data, so the getter returned null and Chrome failed WPT connect-event.html while Firefox and Safari passed. Chrome 160 builds the event with initMessageEvent(…, data = "", …) instead. Interop risk is low because almost nobody reads data on connect — but code that does, and only ever ran in Chrome, changes behaviour.

the API

// shared-worker.js
onconnect = (e) => {
  e.data;               // Chrome ≤159: null     Chrome 160+: ""  (Firefox, Safari: "")
  typeof e.data;        // "object"              "string"
  e.ports.length;       // 1  — unchanged
  e.source === e.ports[0]; // true — unchanged
  const port = e.ports[0]; // the robust way to handle a connection
};

// page.js
const worker = new SharedWorker("shared-worker.js", { name: "hub" });
worker.port.postMessage("hi");

references