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:

  • provides a master directory of all the named caches that can be accessed by a 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)
  • maintains a mapping of string names to corresponding Cache objects

Use 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.

Note: CacheStorage always rejects with a 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.
Note: 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().

Methods

CacheStorage.match()
Checks if a given 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()
Returns a Promise that resolves to true if a Cache object matching the cacheName exists.
CacheStorage.open()
Returns a Promise that resolves to the Cache object matching the cacheName (a new cache is created if it doesn't already exist.)
CacheStorage.delete()
Finds the 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()
Returns a 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.

Examples

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:

  1. Check whether a match for the request is found in the CacheStorage. If so, serve that.
  2. If not, fetch the request from the network, then also open the cache created in the first block and add a clone of the request to it using Cache.put (cache.put(event.request, response.clone()).)
  3. If this fails (e.g. because the network is down), return a fallback response.

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');
  }));
});

Specifications

Specification Status Comment
Service Workers
The definition of 'CacheStorage' in that specification.
Working Draft Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic support
Experimental
Chrome Full support 40
Notes
Full support 40
Notes
Notes Accessible from Window from version 43.
Notes Accessible from WorkerGlobalScope from version 43.
Edge Full support YesFirefox Full support 44
Notes
Full support 44
Notes
Notes Service workers (and Push) have been disabled in the Firefox 45 & 52 Extended Support Releases (ESR.)
IE No support NoOpera Full support 27Safari Full support 11.1WebView Android Full support 40
Notes
Full support 40
Notes
Notes Accessible from Window from version 43.
Notes Accessible from WorkerGlobalScope from version 43.
Chrome Android Full support 40
Notes
Full support 40
Notes
Notes Accessible from Window from version 43.
Notes Accessible from WorkerGlobalScope from version 43.
Edge Mobile Full support YesFirefox Android Full support 44Opera Android Full support 27Safari iOS Full support YesSamsung Internet Android ?
delete
Experimental
Chrome Full support 40Edge Full support YesFirefox Full support 44
Notes
Full support 44
Notes
Notes Service workers (and Push) have been disabled in the Firefox 45 & 52 Extended Support Releases (ESR.)
IE No support NoOpera Full support 27Safari Full support 11.1WebView Android Full support 40Chrome Android Full support 40Edge Mobile Full support YesFirefox Android Full support 44Opera Android Full support 27Safari iOS Full support YesSamsung Internet Android ?
has
Experimental
Chrome Full support 40Edge Full support YesFirefox Full support 44
Notes
Full support 44
Notes
Notes Service workers (and Push) have been disabled in the Firefox 45 & 52 Extended Support Releases (ESR.)
IE No support NoOpera Full support 27Safari Full support 11.1WebView Android Full support 40Chrome Android Full support 40Edge Mobile Full support YesFirefox Android Full support 44Opera Android Full support 27Safari iOS Full support YesSamsung Internet Android ?
keys
Experimental
Chrome Full support 40Edge Full support YesFirefox Full support 44
Notes
Full support 44
Notes
Notes Service workers (and Push) have been disabled in the Firefox 45 & 52 Extended Support Releases (ESR.)
IE No support NoOpera Full support 27Safari Full support 11.1WebView Android Full support 40Chrome Android Full support 40Edge Mobile Full support YesFirefox Android Full support 44Opera Android Full support 27Safari iOS Full support YesSamsung Internet Android ?
match
Experimental
Chrome Full support 54
Full support 54
Partial support 40
Notes
Notes The options parameter only supports ignoreSearch, and cacheName.
Edge Full support YesFirefox Full support 44
Notes
Full support 44
Notes
Notes Service workers (and Push) have been disabled in the Firefox 45 & 52 Extended Support Releases (ESR.)
IE No support NoOpera Full support 41
Full support 41
Partial support 27
Notes
Notes The options parameter only supports ignoreSearch, and cacheName.
Safari Full support 11.1WebView Android Full support 54
Full support 54
Partial support 40
Notes
Notes The options parameter only supports ignoreSearch, and cacheName.
Chrome Android Full support 54
Full support 54
Partial support 40
Notes
Notes The options parameter only supports ignoreSearch, and cacheName.
Edge Mobile Full support YesFirefox Android Full support 44Opera Android Full support 41
Full support 41
Partial support 27
Notes
Notes The options parameter only supports ignoreSearch, and cacheName.
Safari iOS Full support YesSamsung Internet Android ?
open
Experimental
Chrome Full support 40Edge Full support YesFirefox Full support 44
Notes
Full support 44
Notes
Notes Service workers (and Push) have been disabled in the Firefox 45 & 52 Extended Support Releases (ESR.)
IE No support NoOpera Full support 27Safari Full support 11.1WebView Android Full support 40Chrome Android Full support 40Edge Mobile Full support YesFirefox Android Full support 44Opera Android Full support 27Safari iOS Full support YesSamsung Internet Android ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.

See also