This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The CacheStorage interface represents the storage for Cache objects.
The interface:
ServiceWorker or other type of worker or window scope (you’re not limited to only using it with service workers, even though the Service Workers spec defines it)Cache objectsUse CacheStorage.open() to obtain a Cache instance.
Use CacheStorage.match() to check if a given Request is a key in any of the Cache objects that the CacheStorage object tracks.
You can access CacheStorage through the global caches property.
SecurityError on untrusted origins (i.e. those that aren't using HTTPS, although this definition will likely become more complex in the future.) When testing, you can get around this by checking the "Enable Service Workers over HTTP (when toolbox is open)" option in the Firefox Devtools options/gear menu.CacheStorage.match() is a convenience method. Equivalent functionality to match a cache entry can be implemented by opening your cache with CacheStorage.open(), returning the entries it contains with CacheStorage.keys(), and matching the one you want with CacheStorage.match().CacheStorage.match()Request is a key in any of the Cache objects that the CacheStorage object tracks, and returns a Promise that resolves to that match.CacheStorage.has()Promise that resolves to true if a Cache object matching the cacheName exists.CacheStorage.open()Promise that resolves to the Cache object matching the cacheName (a new cache is created if it doesn't already exist.)CacheStorage.delete()Cache object matching the cacheName, and if found, deletes the Cache object and returns a Promise that resolves to true. If no Cache object is found, it returns false.CacheStorage.keys()Promise that will resolve with an array containing strings corresponding to all of the named Cache objects tracked by the CacheStorage. Use this method to iterate over a list of all the Cache objects.This code snippet is from the MDN sw-test example (see sw-test running live.) This service worker script waits for an InstallEvent to fire, then runs waitUntil to handle the install process for the app. This consists of calling CacheStorage.open to create a new cache, then using Cache.addAll to add a series of assets to it.
In the second code block, we wait for a FetchEvent to fire. We construct a custom response like so:
Cache.put (cache.put(event.request, response.clone()).)Finally, return whatever the custom response ended up being equal to, using FetchEvent.respondWith.
this.addEventListener('install', function(event) {
event.waitUntil(
caches.open('v1').then(function(cache) {
return cache.addAll([
'/sw-test/',
'/sw-test/index.html',
'/sw-test/style.css',
'/sw-test/app.js',
'/sw-test/image-list.js',
'/sw-test/star-wars-logo.jpg',
'/sw-test/gallery/bountyHunters.jpg',
'/sw-test/gallery/myLittleVader.jpg',
'/sw-test/gallery/snowTroopers.jpg'
]);
})
);
});
this.addEventListener('fetch', function(event) {
var response;
event.respondWith(caches.match(event.request).catch(function() {
return fetch(event.request);
}).then(function(r) {
response = r;
caches.open('v1').then(function(cache) {
cache.put(event.request, response);
});
return response.clone();
}).catch(function() {
return caches.match('/sw-test/gallery/myLittleVader.jpg');
}));
}); | Specification | Status | Comment |
|---|---|---|
| Service Workers The definition of 'CacheStorage' in that specification. | Working Draft | Initial definition. |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | 40
|
Yes | 44
|
No | 27 | No |
delete
|
40 | Yes | 44
|
No | 27 | No |
has
|
40 | Yes | 44
|
No | 27 | No |
keys
|
40 | Yes | 44
|
No | 27 | No |
match
|
54
|
Yes | 44
|
No | 41
|
No |
open
|
40 | Yes | 44
|
No | 27 | No |
| 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 | ? |
delete
|
40 | 40 | Yes | 44 | 27 | Yes | ? |
has
|
40 | 40 | Yes | 44 | 27 | Yes | ? |
keys
|
40 | 40 | Yes | 44 | 27 | Yes | ? |
match
|
54
|
54
|
Yes | 44 | 41
|
Yes | ? |
open
|
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