Service Worker vs Shared Worker

Lewis picture Lewis · Mar 5, 2015 · Viewed 9.7k times · Source

What is the difference between Service Worker and Shared Worker?

When should I use Service Worker instead of Shared Worker and vice versa?

Answer

Jeff Posnick picture Jeff Posnick · Mar 6, 2015

A service worker has additional functionality beyond what's available in shared workers, and once registered, they persist outside the lifespan of a given web page.

Service workers can respond to message events, like shared workers, but they also have access to additional events. Handling fetch events allows service workers to intercept any network traffic (originating from a controlled page) and take specific actions, including serving responses from a Request/Response cache. There are also plans to expose a push event to service workers, allowing web apps to receive push messages in the "background".

The other major difference relates to persistence. Once a service worker is registered for a specific origin and scope, it stays registered indefinitely. (A service worker will automatically be updated if the underlying script changes, and it can be either manually or programmatically removed, but that's the exception.) Because a service worker is persistent, and has a life independent of the pages active in a web browser, it opens the door for things like using them to power the aforementioned push messaging—a service worker can be "woken up" and process a push event as long as the browser is running, regardless of which pages are active. Future web platform features are likely to take advantage of this persistence as well.

There are other, technical differences, but from a higher-level view, those are what stand out.