Viv*_*ngh 4 caching service-worker
我写了下面的代码来检查特定的 URL 是否已经在 service worker 缓存中?但即使 URL 不存在于缓存中,它也会返回/控制台“在缓存中找到”。
var isExistInCache = function(request){
return caches.open(this.cacheName).then(function(cache) {
return cache.match(request).then(function(response){
debug_("Found in cache "+response,debug);
return true;
},function(err){
debug_("Not found in cache "+response,debug);
return false;
});
})
}
Run Code Online (Sandbox Code Playgroud)
调用上面的函数作为
cache.isExistInCache('http://localhost:8080/myroom.css').then(function(isExist){
console.log(isExist);
})
Run Code Online (Sandbox Code Playgroud)
从Cache.match函数的文档中,promise 总是得到解决。如果未找到匹配项,则使用Response对象或 undefined来解析它。
因此,您必须像这样修改您的函数:
return caches.open(this.cacheName)
.then(function(cache) {
return cache.match(request)
.then(function(response) {
return !!response; // or `return response ? true : false`, or similar.
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1970 次 |
| 最近记录: |