2016-05-12 63 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.match機能の文書から

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

答えて

3

としての機能の上に呼び出すと、約束は必ず解決されます。 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. 
    }); 
}); 
関連する問題