CacheStorage

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Web Workers.

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).
  • 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 Window.caches property in windows or through the WorkerGlobalScope.caches property in workers.

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 on Firefox, 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. Furthermore, because CacheStorage requires file-system access, it may be unavailable in private mode in Firefox.

Note: CacheStorage.match() is a convenience method. Equivalent functionality to match a cache entry can be implemented by returning an array of cache names from CacheStorage.keys(), opening each cache with CacheStorage.open(), and matching the one you want with Cache.match().

Instance 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 resolves to 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 simple service worker example (see simple service worker running live.) This service worker script waits for an install event 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.

js
self.addEventListener("install", (event) => {
  event.waitUntil(
    caches
      .open("v1")
      .then((cache) =>
        cache.addAll([
          "/",
          "/index.html",
          "/style.css",
          "/app.js",
          "/image-list.js",
          "/star-wars-logo.jpg",
          "/gallery/bountyHunters.jpg",
          "/gallery/myLittleVader.jpg",
          "/gallery/snowTroopers.jpg",
        ]),
      ),
  );
});

self.addEventListener("fetch", (event) => {
  event.respondWith(
    caches.match(event.request).then((response) => {
      // caches.match() always resolves
      // but in case of success response will have value
      if (response !== undefined) {
        return response;
      }
      return fetch(event.request)
        .then((response) => {
          // response may be used only once
          // we need to save clone to put one copy in cache
          // and serve second one
          let responseClone = response.clone();

          caches
            .open("v1")
            .then((cache) => cache.put(event.request, responseClone));
          return response;
        })
        .catch(() => caches.match("/gallery/myLittleVader.jpg"));
    }),
  );
});

This snippet shows how the API can be used outside of a service worker context, and uses the await operator for much more readable code.

js
// Try to get data from the cache, but fall back to fetching it live.
async function getData() {
  const cacheVersion = 1;
  const cacheName = `myapp-${cacheVersion}`;
  const url = "https://jsonplaceholder.typicode.com/todos/1";
  let cachedData = await getCachedData(cacheName, url);

  if (cachedData) {
    console.log("Retrieved cached data");
    return cachedData;
  }

  console.log("Fetching fresh data");

  const cacheStorage = await caches.open(cacheName);
  await cacheStorage.add(url);
  cachedData = await getCachedData(cacheName, url);
  await deleteOldCaches(cacheName);

  return cachedData;
}

// Get data from the cache.
async function getCachedData(cacheName, url) {
  const cacheStorage = await caches.open(cacheName);
  const cachedResponse = await cacheStorage.match(url);

  if (!cachedResponse || !cachedResponse.ok) {
    return false;
  }

  return await cachedResponse.json();
}

// Delete any old caches to respect user's disk space.
async function deleteOldCaches(currentCache) {
  const keys = await caches.keys();

  for (const key of keys) {
    const isOurCache = key.startsWith("myapp-");
    if (currentCache === key || !isOurCache) {
      continue;
    }
    caches.delete(key);
  }
}

try {
  const data = await getData();
  console.log({ data });
} catch (error) {
  console.error({ error });
}

Specifications

Specification
Service Workers Nightly
# cachestorage-interface

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
CacheStorage
Chrome – Partial support
Chrome 40 – 42 (Release date: 2015-01-21)
footnote Partial support
footnote Only available in service workers.
Chrome – Full support
Chrome 43 (Release date: 2015-05-19)
footnote Full support
Edge – Full support
Edge 16 (Release date: 2017-10-17)
footnote Full support
Firefox – Full support
Firefox 41 (Release date: 2015-09-22)
footnote Full support
Opera – Partial support
Opera 27 – 29 (Release date: 2015-01-27)
footnote Partial support
footnote Only available in service workers.
Opera – Full support
Opera 30 (Release date: 2015-06-09)
footnote Full support
Safari – Full support
Safari 11.1 (Release date: 2018-04-12)
footnote Full support
Chrome Android – Partial support
Chrome Android 40 – 42 (Release date: 2015-01-21)
footnote Partial support
footnote Only available in service workers.
Chrome Android – Full support
Chrome Android 43 (Release date: 2015-05-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 41 (Release date: 2015-09-22)
footnote Full support
Opera Android – Partial support
Opera Android 27 – 29 (Release date: 2015-01-29)
footnote Partial support
footnote Only available in service workers.
Opera Android – Full support
Opera Android 30 (Release date: 2015-06-10)
footnote Full support
Safari on iOS – Full support
Safari on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 4 (Release date: 2016-03-11)
footnote Full support
WebView Android – Partial support
WebView Android 40 – 42 (Release date: 2015-01-21)
footnote Partial support
footnote Only available in service workers.
WebView Android – Full support
WebView Android 43 (Release date: 2015-05-27)
footnote Full support
WebView on iOS – Full support
WebView on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Deno – Full support
Deno 1.26 (Release date: 2022-09-29)
footnote Full support
delete
Chrome – Full support
Chrome 40 (Release date: 2015-01-21)
footnote Full support
Edge – Full support
Edge 16 (Release date: 2017-10-17)
footnote Full support
Firefox – Full support
Firefox 41 (Release date: 2015-09-22)
footnote Full support
Opera – Full support
Opera 27 (Release date: 2015-01-27)
footnote Full support
Safari – Full support
Safari 11.1 (Release date: 2018-04-12)
footnote Full support
Chrome Android – Full support
Chrome Android 40 (Release date: 2015-01-21)
footnote Full support
Firefox for Android – Full support
Firefox for Android 41 (Release date: 2015-09-22)
footnote Full support
Opera Android – Full support
Opera Android 27 (Release date: 2015-01-29)
footnote Full support
Safari on iOS – Full support
Safari on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 4 (Release date: 2016-03-11)
footnote Full support
WebView Android – Full support
WebView Android 40 (Release date: 2015-01-21)
footnote Full support
WebView on iOS – Full support
WebView on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Deno – Full support
Deno 1.26 (Release date: 2022-09-29)
footnote Full support
has
Chrome – Full support
Chrome 40 (Release date: 2015-01-21)
footnote Full support
Edge – Full support
Edge 16 (Release date: 2017-10-17)
footnote Full support
Firefox – Full support
Firefox 41 (Release date: 2015-09-22)
footnote Full support
Opera – Full support
Opera 27 (Release date: 2015-01-27)
footnote Full support
Safari – Full support
Safari 11.1 (Release date: 2018-04-12)
footnote Full support
Chrome Android – Full support
Chrome Android 40 (Release date: 2015-01-21)
footnote Full support
Firefox for Android – Full support
Firefox for Android 41 (Release date: 2015-09-22)
footnote Full support
Opera Android – Full support
Opera Android 27 (Release date: 2015-01-29)
footnote Full support
Safari on iOS – Full support
Safari on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 4 (Release date: 2016-03-11)
footnote Full support
WebView Android – Full support
WebView Android 40 (Release date: 2015-01-21)
footnote Full support
WebView on iOS – Full support
WebView on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Deno – Full support
Deno 1.26 (Release date: 2022-09-29)
footnote Full support
keys
Chrome – Full support
Chrome 40 (Release date: 2015-01-21)
footnote Full support
Edge – Full support
Edge 16 (Release date: 2017-10-17)
footnote Full support
Firefox – Full support
Firefox 41 (Release date: 2015-09-22)
footnote Full support
Opera – Full support
Opera 27 (Release date: 2015-01-27)
footnote Full support
Safari – Full support
Safari 11.1 (Release date: 2018-04-12)
footnote Full support
Chrome Android – Full support
Chrome Android 40 (Release date: 2015-01-21)
footnote Full support
Firefox for Android – Full support
Firefox for Android 41 (Release date: 2015-09-22)
footnote Full support
Opera Android – Full support
Opera Android 27 (Release date: 2015-01-29)
footnote Full support
Safari on iOS – Full support
Safari on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 4 (Release date: 2016-03-11)
footnote Full support
WebView Android – Full support
WebView Android 40 (Release date: 2015-01-21)
footnote Full support
WebView on iOS – Full support
WebView on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Deno – No support
Deno
footnote No support
match
Chrome – Partial support
Chrome 40 – 53 (Release date: 2015-01-21)
footnote Partial support
footnote The options parameter only supports ignoreSearch, and cacheName.
Chrome – Full support
Chrome 54 (Release date: 2016-10-12)
footnote Full support
Edge – Full support
Edge 16 (Release date: 2017-10-17)
footnote Full support
Firefox – Full support
Firefox 41 (Release date: 2015-09-22)
footnote Full support
Opera – Partial support
Opera 27 – 40 (Release date: 2015-01-27)
footnote Partial support
footnote The options parameter only supports ignoreSearch, and cacheName.
Opera – Full support
Opera 41 (Release date: 2016-10-25)
footnote Full support
Safari – Full support
Safari 11.1 (Release date: 2018-04-12)
footnote Full support
Chrome Android – Partial support
Chrome Android 40 – 53 (Release date: 2015-01-21)
footnote Partial support
footnote The options parameter only supports ignoreSearch, and cacheName.
Chrome Android – Full support
Chrome Android 54 (Release date: 2016-10-19)
footnote Full support
Firefox for Android – Full support
Firefox for Android 41 (Release date: 2015-09-22)
footnote Full support
Opera Android – Partial support
Opera Android 27 – 37 (Release date: 2015-01-29)
footnote Partial support
footnote The options parameter only supports ignoreSearch, and cacheName.
Opera Android – Full support
Opera Android 41 (Release date: 2016-10-25)
footnote Full support
Safari on iOS – Full support
Safari on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Samsung Internet – Partial support
Samsung Internet 4 – 5.4 (Release date: 2016-03-11)
footnote Partial support
footnote The options parameter only supports ignoreSearch, and cacheName.
Samsung Internet – Full support
Samsung Internet 6 (Release date: 2017-08-23)
footnote Full support
WebView Android – Partial support
WebView Android 40 – 53 (Release date: 2015-01-21)
footnote Partial support
footnote The options parameter only supports ignoreSearch, and cacheName.
WebView Android – Full support
WebView Android 54 (Release date: 2016-10-19)
footnote Full support
WebView on iOS – Full support
WebView on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Deno – No support
Deno
footnote No support
open
Chrome – Full support
Chrome 40 (Release date: 2015-01-21)
footnote Full support
Edge – Full support
Edge 16 (Release date: 2017-10-17)
footnote Full support
Firefox – Full support
Firefox 41 (Release date: 2015-09-22)
footnote Full support
Opera – Full support
Opera 27 (Release date: 2015-01-27)
footnote Full support
Safari – Full support
Safari 11.1 (Release date: 2018-04-12)
footnote Full support
Chrome Android – Full support
Chrome Android 40 (Release date: 2015-01-21)
footnote Full support
Firefox for Android – Full support
Firefox for Android 41 (Release date: 2015-09-22)
footnote Full support
Opera Android – Full support
Opera Android 27 (Release date: 2015-01-29)
footnote Full support
Safari on iOS – Full support
Safari on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 4 (Release date: 2016-03-11)
footnote Full support
WebView Android – Full support
WebView Android 40 (Release date: 2015-01-21)
footnote Full support
WebView on iOS – Full support
WebView on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Deno – Full support
Deno 1.26 (Release date: 2022-09-29)
footnote Full support
Secure context required
Chrome – Full support
Chrome 65 (Release date: 2018-03-06)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – Full support
Firefox 44 (Release date: 2016-01-26)
footnote Full support
Opera – Full support
Opera 52 (Release date: 2018-03-22)
footnote Full support
Safari – Full support
Safari 11.1 (Release date: 2018-04-12)
footnote Full support
Chrome Android – Full support
Chrome Android 65 (Release date: 2018-03-06)
footnote Full support
Firefox for Android – Full support
Firefox for Android 44 (Release date: 2016-01-26)
footnote Full support
Opera Android – Full support
Opera Android 47 (Release date: 2018-07-23)
footnote Full support
Safari on iOS – Full support
Safari on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 9 (Release date: 2018-09-15)
footnote Full support
WebView Android – Full support
WebView Android 65 (Release date: 2018-03-06)
footnote Full support
WebView on iOS – Full support
WebView on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Deno – No support
Deno
footnote No support
Available in workers
Chrome – Partial support
Chrome 40 – 42 (Release date: 2015-01-21)
footnote Partial support
footnote Only available in service workers.
Chrome – Full support
Chrome 43 (Release date: 2015-05-19)
footnote Full support
Edge – Full support
Edge 16 (Release date: 2017-10-17)
footnote Full support
Firefox – Full support
Firefox 44 (Release date: 2016-01-26)
footnote Full support
Opera – Partial support
Opera 27 – 29 (Release date: 2015-01-27)
footnote Partial support
footnote Only available in service workers.
Opera – Full support
Opera 30 (Release date: 2015-06-09)
footnote Full support
Safari – Full support
Safari 11.1 (Release date: 2018-04-12)
footnote Full support
Chrome Android – Partial support
Chrome Android 40 – 42 (Release date: 2015-01-21)
footnote Partial support
footnote Only available in service workers.
Chrome Android – Full support
Chrome Android 43 (Release date: 2015-05-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 44 (Release date: 2016-01-26)
footnote Full support
Opera Android – Partial support
Opera Android 27 – 29 (Release date: 2015-01-29)
footnote Partial support
footnote Only available in service workers.
Opera Android – Full support
Opera Android 30 (Release date: 2015-06-10)
footnote Full support
Safari on iOS – Full support
Safari on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 4 (Release date: 2016-03-11)
footnote Full support
WebView Android – Partial support
WebView Android 40 – 42 (Release date: 2015-01-21)
footnote Partial support
footnote Only available in service workers.
WebView Android – Full support
WebView Android 43 (Release date: 2015-05-27)
footnote Full support
WebView on iOS – Full support
WebView on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Deno – Full support
Deno 1.26 (Release date: 2022-09-29)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Partial support
Partial support
No support
No support
Has more compatibility info.

See also