Sim*_*yJS 4 javascript caching web-services
我们caches.match(event.request)在 Service Worker 中使用“仅缓存策略”。我注意到我们也在承诺cache.match('someURL')之后立即返回caches.open("cache-name")。这很令人困惑。
caches.match(event.request)和 和有什么区别cache.match('someURL')?每个的用例是什么?
案例示例:
缓存匹配
self.addEventListener('fetch', function(event) {
event.respondWith(caches.match(event.request));
});
Run Code Online (Sandbox Code Playgroud)
缓存匹配
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.open('mysite-dynamic').then(function(cache) {
return cache.match(event.request).then(function (response) {
return response || fetch(event.request).then(function(response) {
cache.put(event.request, response.clone());
return response;
});
});
})
);
});
Run Code Online (Sandbox Code Playgroud)
小智 6
我猜您不知道“缓存”一词指的是CacheStorage 基本上是缓存或缓存存储存储所有缓存,而缓存只是缓存存储内的命名缓存。基本上caches.match应该为您提供缓存存储的实例,而cache.match为您提供特定缓存的实例。