【发布时间】:2018-01-07 23:53:33
【问题描述】:
我在我的 Service Worker 中使用了以下逻辑(用我自己的话来说):
如果缓存存在,请使用它,但也可以从网络更新缓存以供以后使用
event.respondWith( // on `fetch`
caches.open(CACHE)
.then(function(cache) {
return cache.match(request);
})
.then(function(matching) {
if (matching) {
requestAndUpdateCache(event);
return matching;
}
...
除了使用缓存的响应进行响应之外,我还运行了这个名为 requestAndUpdateCache 的函数。
function requestAndUpdateCache(event){
var url = event.request.url + '?t=' + new Date().getTime();
fetch(url)
.then(function(response){
if (response && response.status === 200){
caches.open(CACHE)
.then(function(cache){
cache.put(event.request, response.clone());
});
}
}, function(error){
console.log(error);
});
}
问题:这个函数及其位置对于完成上述逻辑是否有意义?
【问题讨论】:
标签: javascript caching service-worker