Sharing across tabs
In the shared hosting, every tab that loads the same worker script with the same options reaches one SharedWorker, and so one engine. This page follows its life: the first tab starts it, a second tab joins, a tab closes, and the worker dies.
The first tab
The first tab's new SharedWorker(url, { type: 'module', extendedLifetime: true, name }) makes the browser start the worker. name is the tab's generation key, tscache:<version>:<fetcher module>: a browser finds a SharedWorker by its URL and its name, so only tabs that would run the same worker code reach one worker (Stale scripts after a deploy). The worker takes an exclusive Web Lock under a fresh name, tscache-worker:<uuid>, and holds it until its global scope goes away. It then builds its server and greets the tab with a hello that names the lock. The handshake is described in Startup and fallback.
Later tabs
A later tab constructs a SharedWorker with the same URL, the same options and the same name, and the browser connects it to the running worker. A tab of a deploy that changed the fetcher's URL or the tscache version has another name, so the browser starts another worker for it; the two workers then find each other over a BroadcastChannel, and the older one stops persisting and tells its tabs with superseded. Its port is attached to the same RpcServer, which gives it the next client id. If its init names the module the worker already runs, it shares that load. If it carries a context that is not the identical value (Object.is) the worker holds, the context takes the updateAuth path, so the fetcher's hook hears it. A context that is the same material (sameMaterial: equal primitives, or plain arrays and objects equal member by member) as the one the last refusal named changes nothing, whatever the auth state: auth stays invalid until the next updateAuth, or a joining tab with other material, and a refresh that took effect first stays in place (Auth refresh). If it names another module, its init is refused with a ConfigError and its createClient rejects; with the fetcher in the worker's name, only a connection made without that name gets this far, which is a 0.2.0 page, and a 0.2.0 client sees the message only (clients from 0.3.0 set code: 'fetcher-conflict').
From then on the tabs share everything the engine holds: data, coverage, versions, watermarks, the fetches in flight (a read in one tab can join a fetch started for another, A read, end to end) and the auth state. The server sends each event to every ready port: cacheCleared, persistStopped, authInvalid, and the mergeWarning events of every request.
A tab closes
On pagehide, unless the page is entering the back/forward cache, the client disposes itself. It posts a dispose request, closes its port and withdraws its queued lock request, all in one step. The server answers the request before it forgets the port, but the client does not wait for that answer, which finds the port closed. A port close event, where the browser fires one, also makes the server forget the port. When the last connection goes, the server asks its persistence to write what the debounce still holds (Persistence). The page creates the SharedWorker with extendedLifetime: true; in a browser that supports the option, the worker is kept running for a while after its last tab, so that write can land.
The worker dies
Chromium runs a SharedWorker in the renderer of the tab that created it. When that renderer crashes, the worker goes with it, and the other tabs get no error and no port closure. The lifetime lock covers this: the browser releases the worker's lock with the worker, and grants it to the requests queued on it. Each client then rejects its pending requests and every later call with a TscacheError, releases what it owns, and emits workerLost once.
The page then creates a new client, which starts a new worker. Its memory starts empty: a cache with persist: true restores what the old worker had written to IndexedDB when the page calls client.cache(config) again (Persistence), and every other cache starts empty.
Without Web Locks (outside a secure context) the worker names no lock and the client watches none; a lost worker is then noticed only if the browser fires close on the port. A dedicated worker's client learns of its worker's failure from the worker's error event instead, with the same outcome.
Drawn from:
packages/tscache/src/client/hosting.ts#openSharedpackages/tscache/src/client/hosting.ts#openDedicatedpackages/tscache/src/client/client.ts#Clientpackages/tscache/src/rpc/port-client.ts#PortClientpackages/tscache/src/rpc/server.ts#RpcServerpackages/tscache/src/rpc/server.ts#Persistencepackages/tscache/src/rpc/lifetime.ts#holdLifetimeLockpackages/tscache/src/rpc/lifetime.ts#watchLifetimeLockpackages/tscache/src/entries/worker.ts#sharedWorkerScopepackages/tscache/src/entries/worker.ts#createServerpackages/tscache/src/orchestrator/orchestrator.ts#Orchestratorpackages/tscache/src/orchestrator/auth.ts#sameMaterialpackages/tscache/src/persist/hosted.ts#HostedPersistence