Show HN: Access OPFS from multiple tabs using a fake Shared Worker
The article introduces cross-tab-worker, a library that enables coordinated access to a single Worker across multiple browser tabs for efficient OPFS usage. It uses a leader-follower model where one tab owns the Worker and others relay messages through a MessagePort. The solution supports zero-copy data transfers and is designed for high-performance WASM applications like SQLite.
- ▪cross-tab-worker ensures only one Worker is active across multiple tabs, with leadership transferred when the owning tab closes.
- ▪It uses a regular dedicated Worker combined with Web Locks and a minimal SharedWorker for coordination, bypassing SharedWorker's lack of support for FileSystemSyncAccessHandle.
- ▪Messages and transferable objects are relayed with zero-copy semantics between tabs, enabling efficient communication for large data operations.
- ▪The library exposes a standard postMessage/onmessage interface, making it transparent to the application whether it is in the leader or follower role.
- ▪It is particularly useful for WASM-based SQLite implementations that require synchronous OPFS access across multiple tabs.
Opening excerpt (first ~120 words) tap to expand
cross-tab-worker A drop-in coordination wrapper that keeps exactly one Worker alive across all same-origin browser tabs. Useful for OPFS access from Workers when using multiple tabs. Exactly one tab owns the real Worker at a time (the leader). All other tabs (followers) send messages through a direct MessagePort to the leader, which forwards them to the worker. When the leader tab closes, one follower is automatically elected as the new leader. The application sees the same postMessage / onmessage interface regardless of role. npm install @arnoldgraf/cross-tab-worker --save Why not SharedWorker? SharedWorker doesn't support FileSystemSyncAccessHandle — the synchronous OPFS API that high-performance SQLite/WASM VFS implementations require.
…
Excerpt limited to ~120 words for fair-use compliance. The full article is at GitHub.