Service Worker 和 AppCache 的比较 [英] Comparison between service worker and AppCache

查看:45
本文介绍了Service Worker 和 AppCache 的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Service Worker 和 AppCache 的核心区别是什么.每种方法的优缺点是什么,什么时候更喜欢一个.

What are the core differences between service worker and AppCache. What are the pros and cons of each and when to prefer one over another .

推荐答案

主要区别在于 AppCache 是一个高级的声明式 API,您可以使用它指定希望浏览器缓存的资源集;而 Service Worker 是一个低级、命令式、事件驱动的 API,您可以使用它编写一个脚本,该脚本可以拦截获取事件并缓存它们的响应以及执行其他操作(例如显示推送通知).

The primary difference is that AppCache is a high-level, declarative API, with which you specify the set of resources you'd like the browser to cache; whereas Service Worker is a low-level, imperative, event-driven API with which you write a script that can intercept fetch events and cache their responses along with doing other things (like displaying push notifications).

优缺点很大程度上取决于API设计的功能:理论上,AppCache更易于使用,但用例更有限;而 Service Worker 更难使用,但更灵活.

The pros and cons are largely a function of API design: theoretically, AppCache is easier to use, while having more limited use cases; whereas Service Worker is harder to use, but is more flexible.

尽管如此,由于设计不佳,AppCache 在实践中被认为难以使用(参见 Application Cache Is A Douchebag 获取设计问题列表).它已被弃用,因此正在从浏览器中删除它(根据 使用应用程序缓存).

Nevertheless, AppCache is considered hard to use in practice due to poor design (see Application Cache Is A Douchebag for a list of design issues). And it has been deprecated, so it is being removed from browsers (per Using the application cache).

因此,喜欢 AppCache 的唯一原因是在尚不支持 Service Worker 的浏览器上离线应用程序,正如 Kenneth Ormandy 在 不要等待 ServiceWorker:使用 One-Line 添加离线支持.

Thus the only reason to prefer AppCache is to offline an app on browsers that don't yet support Service Worker, as Kenneth Ormandy recommends in Don’t Wait for ServiceWorker: Adding Offline Support with One-Line.

比较 我可以使用 Service Worker 吗?我可以使用离线 web 应用程序吗? 查看浏览器支持的差异.但请注意,支持 Service Worker 的浏览器(如 Chrome 和 Firefox)正在取消对 AppCache 的支持,因此您需要同时实现这两项功能,以便在支持任一标准的所有浏览器中离线您的应用.

Compare Can I use Service Workers? to Can I use Offline web applications? to see the differences in browser support. But note that browsers that support Service Worker, like Chrome and Firefox, are removing support for AppCache, so you'll need to implement both to offline your app across all browsers that support either standard.

这篇关于Service Worker 和 AppCache 的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆