This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The has()
method of the CacheStorage
interface returns a Promise
that resolves to true
if a Cache
object matches the cacheName
.
caches.has(cacheName).then(function(boolean) { // true: your cache exists! });
a Promise
that resolves to true
if the cache exists or false if not.
DOMString
representing the name of the Cache
object you are looking for in the CacheStorage
.The following example first checks whether a cache called 'v1' exists. If so, we add a list of assets to it. If not (meaning the has()
promise would reject) then we run some kind of cache set-up function.
caches.has('v1').then(function(hasCache) { if (!hasCache) { someCacheSetupfunction(); } else { caches.open('v1').then(function(cache) { return cache.addAll(myAssets); }); } }).catch(function() { // Handle exception here. });
Specification | Status | Comment |
---|---|---|
Service Workers The definition of 'CacheStorage: has' in that specification. | Working Draft | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 40 | Yes | 44
|
No | 27 | 11.1 |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 40 | 40 | Yes | 44 | 27 | Yes | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage/has