This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The open() method of the CacheStorage interface returns a Promise that resolves to the Cache object matching the cacheName.

Note: If the specified Cache does not exist, a new cache is created with that cacheName and a Promise that resolves to this new Cache object is returned.

Syntax

// "caches" is a global read-only variable, which is an instance of CacheStorage,
// For more info, refer to: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/caches

caches.open(cacheName).then(function(cache) {
  // Do something with your cache
});

Parameters

cacheName
The name of the cache you want to open.

Return value

A Promise that resolves to the requested Cache object.

Examples

This example is from the MDN sw-test example (see sw-test running live). Here we wait for a FetchEvent to fire. Then we construct a custom response like so:

  1. Check whether a match for the request is found in the CacheStorage using CacheStorage.match(). If so, serve that.
  2. If not, open the v1 cache using CacheStorage.open(), put the default network request in the cache using Cache.put() and return a clone of the default network request using return response.clone(). The last is necessary because put() consumes the response body.
  3. If this fails (e.g., because the network is down), return a fallback response.
var cachedResponse = caches.match(event.request).catch(function() {
  return fetch(event.request);
}).then(function(response) {
  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: open' in that specification.
Working Draft Initial definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic support
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