2016-05-12 114 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. 
    }); 
});