api 缓存
The awesome ServiceWorker API is meant to give developers a bit more control over what is and isn't cached, and how. Sure we can play games with ETags and the like but doing it programmatically with JavaScript just feels better, more controllable. As with every API, however, adding stuff to cache isn't just fun and games -- you have to do the cleanup work yourself too, and by "cleanup work" I mean having to delete cache.
出色的ServiceWorker API旨在使开发人员对缓存的内容和不缓存的内容以及如何进行更多控制。 当然,我们可以使用ETags等玩游戏,但是使用JavaScript以编程方式进行操作会感觉更好,更可控。 但是,与每个API一样,向缓存中添加内容不仅很有趣,而且也很有趣-您也必须自己进行清理工作,而通过“清理工作”,我的意思是必须删除缓存。
Let's have a look at how you can get caches, add and delete requests from caches, and how you can then delete an entire cache!
让我们看一下如何获取缓存,从缓存中添加和删除请求以及如何删除整个缓存!
检测cache
API (Detecting the cache
API)
To detect if the browser supports the Cache API...
要检测浏览器是否支持Cache API ...
if('caches' in window) {
// Has support!
}
...check for the presence of the caches
object within the window
.
...检查window
是否存在caches
对象。
创建一个缓存 (Creating a Cache)
Creating a cache requires a caches.open
call with a cache name:
创建缓存需要使用缓存名称进行caches.open
调用:
caches.open('test-cache').then(function(cache) {
// Cache is created and accessible
});
The caches.open
call returns a