Shared Workers: One Background Script for Multiple Tabs

A SharedWorker is a single background script shared across multiple tabs or windows from the same origin. Use it to manage a single WebSocket connection or sync state between pages. The footgun: each tab must call `port.start()` on its own message port.
Think of a SharedWorker as a singleton background service for your web origin. It's a single JavaScript thread that multiple browser tabs, windows, or iframes can connect to. This is ideal for managing a single resource, like a WebSocket connection, or for synchronizing state across multiple open pages. The main footgun is forgetting that each connecting script gets its own `MessagePort` and must explicitly call `port.start()` on it to enable communication.
Read the original → developer.mozilla.org
- #web workers
- #javascript
- #web apis
- #concurrency
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.