2016-05-12 62 views
1

我已经编写了下面的代码来检查特定的URL是否已经在服务工作者缓存中?但即使该URL不在缓存中,它也会返回/“控制台在缓存中找到”。调用上面的函数检查服务工作者缓存中是否存在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; 
     }); 
     }) 
} 

cache.isExistInCache('http://localhost:8080/myroom.css').then(function(isExist){ 
     console.log(isExist); 
    }) 

回答

3

Cache.match函数的文档,承诺始终解决。它通过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. 
    }); 
});