问题描述
服务工作者和共享工作者之间有什么区别?
What is the difference between Service Worker and Shared Worker?
我应该何时使用服务工作者而不是共享工作者,反之亦然?
When should I use Service Worker instead of Shared Worker and vice versa?
推荐答案
服务工作者具有除共享工作者之外的其他功能,并且一旦注册,它们将在给定网页的生命周期之外持续存在。
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.
服务工作者可以响应消息
事件,例如共享工作者,但他们也可以访问其他事件。处理 fetch
事件允许服务工作者拦截任何网络流量(源自受控页面)并采取特定操作,包括提供来自请求的响应
/ 响应
缓存。还有公开将
事件推送到服务工作者,允许网络应用程序在后台接收推送消息。
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".
另一个主要区别与持久性有关。一旦服务工作者注册了特定的来源和范围,它就会无限期地保持注册状态。 (如果底层脚本发生更改,服务工作者将自动更新,并且可以手动或以编程方式删除,但这是例外。)因为服务工作者是持久的,并且具有独立于Web浏览器中活动页面的生命,它为诸如使用它们来支持上述推送消息传递之类的东西打开了大门 - 只要浏览器是一个服务工作者就可以醒来并处理推送
事件无论哪个页面处于活动状态,都可以运行未来的网络平台功能也可能利用这种持久性。
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.
这篇关于服务工作者与共享工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!