Skip to content
tezvyn:

Shared Workers: One Background Script for Multiple Tabs

Source: developer.mozilla.orgHardHow cards are made

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.

Why it exists

Web applications often need to coordinate state or perform background tasks across multiple open tabs or windows for the same site. Without a shared resource, each tab would run its own duplicate logic, wasting resources and making state synchronization complex and error-prone.

The mental model

Think of a SharedWorker as a singleton background service for your web origin. Instead of each browser tab having its own private assistant (a DedicatedWorker), all tabs from your site can talk to the same shared assistant. This is ideal for managing a single resource, like a WebSocket connection, or a shared cache across multiple open pages.

How it works

A script in a tab creates a SharedWorker pointing to a JS file: const myWorker = new SharedWorker('worker.js');. Other tabs from the same origin can connect to this existing worker instance. Each connecting script gets its own unique MessagePort via the worker.port property to communicate. Communication is two-way: scripts use port.postMessage() to send data, and the worker uses the corresponding port to message back. The worker itself runs in a SharedWorkerGlobalScope, separate from any window's main thread.

When to use it

Use a SharedWorker when you need to manage a single, shared state or resource across multiple browsing contexts. Examples include: managing a single WebSocket connection for all open tabs, synchronizing application state between an app and a pop-out window, or centralizing polling for updates to avoid redundant network requests.

When not to use it

Don't use a SharedWorker for tasks that are specific to a single tab. A DedicatedWorker is simpler and more appropriate for offloading work from a single UI thread. Also, be aware that browser support can be inconsistent compared to other worker types. For some offline or advanced caching use cases, a ServiceWorker might be a more robust and widely supported alternative.

One canonical example

Two browser tabs for example.com need to share data. Tab A creates the worker: const myWorker = new SharedWorker('worker.js');. Tab B runs the same code and connects to the existing worker. Both tabs must then get their port (myWorker.port) and, critically, call port.start() to begin sending and receiving messages. The worker script can then manage messages from both tabs, keeping a shared state or relaying messages between them.

Interview question

Which scenario best highlights the unique benefit of a SharedWorker compared to other worker types?

  • a.Enabling secure, direct data exchange between a website and an iframe from a different domain.
  • b.Caching static assets and intercepting network requests for offline use.
  • c.Performing complex image processing without freezing the UI of a single tab.
  • d.Maintaining a single, live WebSocket connection accessible by multiple open tabs.Correct
Why?

The correct answer highlights the SharedWorker's unique ability to manage a single resource across multiple tabs from the same origin, as described in the card. Other options describe use cases for DedicatedWorkers (C), ServiceWorkers (B), or incorrect cross-origin communication (A).

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles