← Chrome 160 reference · All APIs
v160 · workers · enabled by default
SharedWorkerConnectEventEmptyStringData
Chrome 160 aligns the SharedWorkerGlobalScope connect event with the HTML Living Standard by initializing MessageEvent.data to the empty string ("") instead of null. Firefox and Safari already initialize connect event data to "", making the event payload interoperable across all three browser engines.
at a glance
| Shipped in | Chrome 160 (desktop, Android, Android WebView; iOS null) — filed under “Enabled by default” in the milestone=160 listing |
|---|---|
| Runtime flag | SharedWorkerConnectEventEmptyStringData, status stable in Blink’s runtime_enabled_features.json5 (kill switch for crbug 583449815; no origin trial, no chrome://flags toggle) |
| Specification | HTML Living Standard — Run a worker · SharedWorker() constructor · MessageEvent |
| Blink source | SharedWorkerGlobalScope::Connect() in shared_worker_global_scope.cc |
| Intent | blink-dev Intent to Ship |
| Tracking bug | crbug.com/583449815 |
| Web platform tests | workers/constructors/SharedWorker/connect-event.html (test source · worker script) |
| ChromeStatus | 5142074717503488 — SharedWorkerConnectEventEmptyStringData (component Blink>Workers) |
why it changed
Both the Run a worker algorithm (for the initial client that starts a SharedWorker) and the SharedWorker() constructor (for subsequent clients attaching to an existing worker) require the user agent to fire a MessageEvent named connect with its data attribute initialized to the empty string (""). Chromium previously constructed the event using a legacy internal overload of MessageEvent::Create() that left data uninitialized (serialized as null to JavaScript), failing WPT connect-event.html while Firefox and Safari passed.
shared_worker_global_scope.cc lines 193–221.
Syntax
No new interfaces or attributes are added. The change updates the initialized value of MessageEvent.data on connect events dispatched at SharedWorkerGlobalScope (replaces pre-160 null with ""):
[Global=(Worker,SharedWorker), Exposed=SharedWorker]
interface SharedWorkerGlobalScope : WorkerGlobalScope {
[Replaceable] readonly attribute DOMString name;
undefined close();
attribute EventHandler onconnect;
};
[Exposed=(Window,Worker,AudioWorklet)]
interface MessageEvent : Event {
readonly attribute any data;
readonly attribute USVString origin;
readonly attribute DOMString lastEventId;
readonly attribute MessageEventSource? source;
readonly attribute FrozenArray<MessagePort> ports;
};
“…fire an event named
connectat worker global scope, usingMessageEvent, with thedataattribute initialized to the empty string, theportsattribute initialized to a new frozen array containing inside port, and thesourceattribute initialized to inside port.”
This reference covers three parts of the contract: MessageEvent.data initialization to "", the full SharedWorkerGlobalScope connect event dispatch contract (onconnect, ports, source), and migrating code that checked for null.
shared_worker_global_scope.idl; Blink message_event.idl.
MessageEvent.data on connect events — inputs and outputs
Inputs. A connect event is triggered whenever a browsing context calls new SharedWorker(scriptURL, options) on the same origin and matching worker name (creating a new worker or attaching to an already running one). The caller cannot pass a custom payload to connect; the browser constructs the MessageEvent internally.
Outputs. When Chrome 160 dispatches the connect event inside SharedWorkerGlobalScope::Connect(), Blink calls event->initMessageEvent("connect", false, false, g_empty_string, nullptr, String(), port, [port]). Script reading event.data observes:
event.data === ""(strictly equal to the empty string) andtypeof event.data === "string".event.data === nullis nowfalse(it wastruein Chrome ≤ 159).Boolean(event.data)remainsfalse(both""andnullare falsy), while nullish coalescing (event.data ?? fallback) now evaluates to""instead offallback!
SharedWorkerGlobalScope::Connect(); WPT connect-event.js.
SharedWorkerGlobalScope connect event fields & lifecycle
Inputs and outputs. Every connect event delivered to self.onconnect or self.addEventListener("connect", handler) is a trusted MessageEvent carrying the newly entangled client MessagePort:
| Property | Chrome 160 value | Chrome ≤ 159 value | Notes |
|---|---|---|---|
event.type | "connect" | "connect" | Unchanged |
event.data | "" (empty string) | null | Changed in Chrome 160 to match HTML spec, Firefox, and Safari |
event.ports | Frozen array [insidePort] (length 1) | Frozen array [insidePort] | Contains the worker-side entangled MessagePort |
event.source | insidePort (same object as event.ports[0]) | insidePort | Strict identity event.source === event.ports[0] |
event.origin | "" | "" | Unchanged (empty string) |
event.lastEventId | "" | "" | Unchanged (empty string) |
event.bubbles / cancelable | false / false | false / false | Non-bubbling, non-cancelable |
Lifecycle and ordering. On first startup, the worker script executes to completion, then the initial connect event is queued and dispatched; subsequent new SharedWorker() calls from other tabs/windows queue additional connect events on the same SharedWorkerGlobalScope. Assign self.onconnect = ... or call self.addEventListener("connect", ...) synchronously during top-level evaluation so the initial connect event is not missed. Remember that assigning port.onmessage implicitly starts the port’s message queue, whereas using port.addEventListener("message", ...) requires calling port.start() explicitly.
SharedWorker(); Blink shared_worker_global_scope.cc.
Migrating from pre-160 null checks
Inputs and outputs. Most shared workers never inspect event.data inside onconnect and instead read event.ports[0] or event.source directly. Code is affected only if it relies on Chromium’s non-standard pre-160 event.data === null behavior — for example:
- Strict equality guards such as
if (event.data === null)orif (event.data !== null)to distinguish browserconnectevents from syntheticnew MessageEvent("connect", { data: ... })events. - Nullish coalescing such as
const tag = event.data ?? "connect": in Chrome ≤ 159 this yielded"connect"; in Chrome 160, Firefox, and Safari it yields""because""is not nullish. - JSON / schema serializers that typed
connectevent snapshots asdata: null.
To remain compatible across both Chrome ≤ 159 and spec-compliant engines (Chrome 160+, Firefox 65+, Safari 16+), identify client connections via event.ports[0] (or event.source) rather than event.data === null, and check event.data === "" || event.data === null if an explicit empty-payload assertion is required.
Errors, edge cases, and synthetic events
This change introduces no new exceptions or error paths on SharedWorkerGlobalScope or onconnect. Note two edge cases:
- Synthetic
new MessageEvent("connect"): Callingnew MessageEvent("connect")directly from script without adataproperty inMessageEventInitstill initializesdatatonullperMessageEventInitdefaultany data = null. Only browser-dispatchedSharedWorkerGlobalScopeconnectevents initializedatato"". - Empty
portsguard: Always verifyevent.ports.length > 0(or checkevent.source instanceof MessagePort) before callingport.postMessage()if your worker can also receive syntheticdispatchEvent(new MessageEvent("connect"))calls in unit tests.
MessageEventInit; WPT connect-event.html.
Context and exposure
SharedWorkerGlobalScope is exposed exclusively inside shared worker execution contexts ([Global=(Worker,SharedWorker), Exposed=SharedWorker]), instantiated from same-origin Window contexts via new SharedWorker(url, options). No permission prompt or user activation is required. Shared workers are same-origin isolated and partitioned by storage key; in Chrome, desktop has supported SharedWorker since Chrome 4 and Chrome for Android since Chrome 148.
shared_worker_global_scope.idl; BCD api/SharedWorkerGlobalScope.json.
Examples
event.data, event.ports, and event.source on a live SharedWorker connect event. Sibling concept demos (all HEAD 200): connection hub, null sentinel migration, and synthetic connect fidelity.Source: chrome-platform-showcaseSpec-compliant onconnect handler in a shared worker
// shared-worker.js (runs in SharedWorkerGlobalScope)
const clients = new Set();
self.onconnect = (event) => {
const [port] = event.ports; // equivalent to event.source
if (!port) return;
// In Chrome 160+, Firefox 65+, and Safari 16+:
// event.data === "" (was null in Chrome <= 159)
clients.add(port);
port.onmessage = (msg) => {
for (const client of clients) {
client.postMessage(msg.data);
}
};
port.postMessage({
type: "connected",
connectData: event.data,
isEmptyString: event.data === "",
portMatchesSource: event.source === port,
});
};
Source: WPT connect-event.js; HTML Living Standard.
Replacing fragile event.data === null checks
// BEFORE (broke on Firefox/Safari and breaks on Chrome 160+):
self.addEventListener("connect", (event) => {
if (event.data !== null) return; // WRONG: event.data is "" in Chrome 160+
const label = event.data ?? "default-connect"; // yields "" in Chrome 160+
handlePort(event.ports[0], label);
});
// AFTER (interoperable across Chrome <= 159, Chrome 160+, Firefox, and Safari):
self.addEventListener("connect", (event) => {
const port = event.ports[0] ?? event.source;
if (!port) return;
const label = event.data || "default-connect"; // handles both "" and legacy null
handlePort(port, label);
});
Source: ChromeStatus 5142074717503488; BCD api/SharedWorkerGlobalScope.json.
Browser compatibility
| Browser | data === "" | Base connect event (BCD) | Notes |
|---|---|---|---|
| Chrome (desktop) | 160 | 4 | Chrome 4–159 initialized event.data to null; Chrome 160 enables SharedWorkerConnectEventEmptyStringData by default |
| Chrome (Android, WebView) | 160 | 148 | SharedWorker shipped on Android in 148; data === "" enabled by default in 160 per ChromeStatus API record |
| Edge | 160 (mirror) | 79 | Chromium-based; BCD mirrors Chrome |
| Firefox | 65 | 29 | BCD note: Firefox 29–64 used null; Firefox 65+ initializes data to "" and passes WPT |
| Safari (macOS, iOS) | 16 | 16 | Stable Safari passes WPT connect-event.html per ChromeStatus |
Security and privacy
Changing the initial value of connect event data from null to "" introduces no new cross-origin capability, permission surface, storage channel, or fingerprinting signal. The worker remains bound to the same-origin and storage-partitioning rules of SharedWorker, and the empty string carries no client or browser state. ChromeStatus records both Security and Privacy reviews as Not applicable for this specification-conformance fix.