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
-
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
connectevent back:dataand itstypeof,origin,lastEventId, the frozenportsarray,source === ports[0],isTrusted. A verdict tells you whether this browser follows the spec. -
Connection hub
A practical presence hub: every page (and every tab) that connects is logged by the worker with the
dataof its ownconnectevent. The first connection comes from “run a worker”, later ones from theSharedWorkerconstructor — the spec says""for both, and the roster shows it per connection. -
Null-sentinel migration
Eight handler patterns found in real worker code, executed against the real event inside the worker.
e.data === null,e.data ?? defaultandtypeof e.data === 'object'flip branch between Chrome 159 and 160;!e.dataande.type === 'connect'don’t. See what your browser does and what to migrate to. -
Synthetic connect fidelity
For test harnesses that fake
connectevents: build candidates withnew MessageEvent('connect', init)inside the worker and diff them field by field against the real one. The constructor’s defaultdatais stillnull— only the browser-fired event changed — so a fake must passdata: ""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
- ChromeStatus entry
- HTML — run a worker (first connect event)
- HTML — SharedWorker constructor (later connect events)
- crbug.com/583449815 — implementation bug
- WPT: workers/constructors/SharedWorker/connect-event.html
- Chromium: SharedWorkerGlobalScope::Connect
- MDN: SharedWorkerGlobalScope connect event