SharedWorkerTransport
Cross-tab delivery through one SharedWorker per origin.
BroadcastChannel is the better default for pure fan-out and stays the default. This transport buys something else: a single place that is not a tab. The relay outlives any individual tab, so the thing every “the leader owns the socket” design actually wants — one connection, held somewhere no user can close mid-flight — becomes possible without electing a leader at all.
What it does not buy: durability. The worker is torn down when the last port closes, exactly like a BroadcastChannel with no listeners. State still lives in the tabs; this moves the wire, not the source of truth.
The relay script is three lines and ships with the library:
// sw-relay.js — your app hosts this fileimport 'use-everywhere/shared-worker';createSharedStore('cart', { transport: new SharedWorkerTransport({ url: new URL('./sw-relay.js', import.meta.url), }),});Implements
Section titled “Implements”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new SharedWorkerTransport(options): SharedWorkerTransport;Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
options | SharedWorkerTransportOptions |
Returns
Section titled “Returns”SharedWorkerTransport
Properties
Section titled “Properties”readonly kind: TransportKind = 'shared-worker';What this transport is. Optional so a custom transport need not declare one.
Implementation of
Section titled “Implementation of”Methods
Section titled “Methods”close()
Section titled “close()”close(): void;Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”post()
Section titled “post()”post(data): void;Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
data | unknown |
Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”subscribe()
Section titled “subscribe()”subscribe(listener): () => void;Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
listener | (data) => void |
Returns
Section titled “Returns”() => void