Service Worker 可以做什么 Web Worker 不能做的事情?

What can service workers do that web workers cannot?

Service Worker 可以做什么 Web Worker 不能做的?或者反之亦然?

网络工作者似乎是服务工作者功能的一个子集。这是正确的吗?

它们的用途有很大的不同:

网络工作者

Web Workers provide a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering with the user interface. In addition, they can perform I/O using XMLHttpRequest (although the responseXML and channel attributes are always null). Once created, a worker can send messages to the JavaScript code that created it by posting messages to an event handler specified by that code (and vice versa.)

Source - Using Web Workers

服务工作者

Service workers essentially act as proxy servers that sit between web applications, and the browser and network (when available). They are intended to (amongst other things) enable the creation of effective offline experiences, intercepting network requests and taking appropriate action based on whether the network is available and updated assets reside on the server. They will also allow access to push notifications and background sync APIs.

Source - Service Worker API

因此 Web Workers 可以方便地处理 运行 昂贵的脚本,而不会导致用户界面冻结, 而 Service Worker 可用于修改网络请求的响应(例如,在构建离线应用程序时)。

是正确的,但在我看来它没有回答原始问题,即:"What can service workers do that web workers cannot? Or vice versa?"

它们的生命周期和您可以拥有的每个来源的实例数量存在根本差异。简而言之:

               | Web Workers  | Service Workers  |
|--------------|--------------|------------------|
| Instances    | Many per tab | One for all tabs |
| Lifespan     | Same as tab  | Independent      |
| Intended use | Parallelism  | Offline support  |

Buksy 的回答基本上是table 的最后一行。致谢:我从 Demystifying Web Workers and Service Workers by Nolan Lawson, starting from slide 35.

中获取了这个 table

特别是,这是生成和终止网络工作者的方式:

Using Web Workers

而服务人员有自己的生命周期,这是公认的 "most complicated part":

The Service Worker Lifecycle

所以生命周期是两者之间的一个根本区别(它们的预期用途的结果)。

浏览器支持曾经有很大的不同:在 Safari iOS 直到 11.3(2018 年 3 月 29 日)之前,Service workers 根本不可用,请参阅Can I use service workers? In contrast, web workers had a much better browser support already in 2012: Can I use web workers?

如果非要支持IE11,只能用web workers:IE11没有service workers,显然end of support for IE11 is October 14, 2025.

不同浏览器的 API 支持 存在细微差别,请参阅 HTML5 Worker Test(也由 Nolan Lawson 撰写)。在特定的浏览器中,一种 worker 可能支持某个 API 调用,而另一种则不支持。访问该页面并测试您自己的浏览器!

服务人员

服务工作者是浏览器和网络之间的代理。通过拦截文档发出的请求,服务工作者可以将请求重定向到缓存,从而实现离线访问。

/* main.js */

navigator.serviceWorker.register('/service-worker.js');



/* service-worker.js */

// Install 
self.addEventListener('install', function(event) {
    // ...
});

// Activate 
self.addEventListener('activate', function(event) {
    // ...
});

// Listen for network requests from the main document
self.addEventListener('fetch', function(event) {
    // ...
});

网络工作者

Web worker 是通用脚本,使我们能够从主线程卸载处理器密集型工作。

/* main.js */

// Create worker
const myWorker = new Worker('worker.js');

// Send message to worker
myWorker.postMessage('Hello!');

// Receive message from worker
myWorker.onmessage = function(e) {
  console.log(e.data);
}

原版PostHere