Diese Übersetzung ist unvollständig. Bitte helfen Sie uns, diesen Artikel aus dem Englischen zu übersetzen

Dies ist eine experimentelle Technologie
Da diese Technologie noch nicht definitiv implementiert wurde, sollte die Browserkompatibilität beachtet werden. Es ist auch möglich, dass die Syntax in einer späteren Spezifikation noch geändert wird.

The Cache interface provides a storage mechanism for Request / Response object pairs that are cached, for example as part of the ServiceWorker life cycle. Note that the Cache interface is exposed to windowed scopes as well as workers. You don't have to use it in conjunction with service workers, even though it is defined in the service worker spec.

An origin can have multiple, named Cache objects. You are responsible for implementing how your script (e.g. in a ServiceWorker)  handles Cache updates. Items in a Cache do not get updated unless explicitly requested; they don’t expire unless deleted. Use to open a specific named Cache object and then call any of the Cache methods to maintain the Cache.

You are also responsible for periodically purging cache entries. Each browser has a hard limit on the amount of cache storage that a given origin can use. The browser does its best to manage disk space, but it may delete the Cache storage for an origin.  The browser will generally delete all of the data for an origin or none of the data for an origin. Make sure to version caches by name and use the caches only from the version of the script that they can safely operate on. See Deleting old caches for more information.

Note: Cache.putCache.add, and Cache.addAll only allow GET requests to be stored in the cache.

Note: Initial Cache implementations (in both Blink and Gecko) resolve Cache.add, Cache.addAll, and Cache.put promises when the response body is fully written to storage.  More recent spec versions have newer language stating that the browser can resolve the promise as soon as the entry is recorded in the database even if the response body is still streaming in.

Note: As of Chrome 46, the Cache API will only store requests from secure origins, meaning those served over HTTPS.

Note: The key matching algorithm depends on the VARY header in the value. So matching a new key requires looking at both key and value for entries in the Cache.

Note: The caching API doesn't honor HTTP caching headers.


Cache.match(request, options)
Returns a Promise that resolves to the response associated with the first matching request in the Cache object.
Cache.matchAll(request, options)
Returns a Promise that resolves to an array of all matching requests in the Cache object.
Takes a URL, retrieves it and adds the resulting response object to the given cache. This is fuctionally equivalent to calling fetch(), then using Cache.put() to add the results to the cache.
Takes an array of URLs, retrieves them, and adds the resulting response objects to the given cache.
Cache.put(request, response)
Takes both a request and its response and adds it to the given cache.
Cache.delete(request, options)
Finds the Cache entry whose key is the request, and if found, deletes the Cache entry and returns a Promise that resolves to true. If no Cache entry is found, it returns false.
Cache.keys(request, options)
Returns a Promise that resolves to an array of Cache keys.


This code snippet is from the service worker selective caching sample. (see selective caching live) The code uses to open any Cache objects with a Content-Type header that starts with font/.

The code then uses Cache.match(request, options) to see if there's already a matching font in the cache, and if so, returns it. If there isn't a matching font, the code fetches the font from the network and uses Cache.put(request, response) to cache the fetched resource.

The code handles exceptions thrown from the fetch() operation. Note that a HTTP error response (e.g., 404) will not trigger an exception. It will return a normal response object that has the appropriate error code set.

The code snippet also shows a best practice for versioning caches used by the service worker. Though there's only one cache in this example, the same approach can be used for multiple caches. It maps a shorthand identifier for a cache to a specific, versioned cache name. The code also deletes all caches that aren't named in CURRENT_CACHES.

Note: In Chrome, visit chrome://inspect/#service-workers and click on the "inspect" link below the registered service worker to view logging statements for the various actions the service-worker.js script is performing.

// Shorthand identifier mapped to specific versioned cache.
  font: 'font-cache-v' + CACHE_VERSION

self.addEventListener('activate', function(event) {
  var expectedCacheNames = Object.keys(CURRENT_CACHES).map(function(key) {
    return CURRENT_CACHES[key];

  // Active worker won't be treated as activated until promise resolves successfully.
    caches.keys().then(function(cacheNames) {
      return Promise.all( {
          if (expectedCacheNames.indexOf(cacheName) == -1) {
            console.log('Deleting out of date cache:', cacheName);
            return caches.delete(cacheName);

self.addEventListener('fetch', function(event) {
  console.log('Handling fetch event for', event.request.url);

    // Opens Cache objects that start with 'font'.['font']).then(function(cache) {
      return cache.match(event.request).then(function(response) {
        if (response) {
          console.log(' Found response in cache:', response);

          return response;
      }).catch(function(error) {
        // Handles exceptions that arise from match() or fetch().
        console.error('  Error in fetch handler:', error);

        throw error;


Specification Status Comment
Service Workers
Die Definition von 'Cache' in dieser Spezifikation.
Arbeitsentwurf Initial definition.

Browser compatibility

Wir konvertieren die Kompatibilitätsdaten in ein maschinenlesbares JSON Format. Diese Kompatibilitätstabelle liegt noch im alten Format vor, denn die darin enthaltenen Daten wurden noch nicht konvertiert. Finde heraus wie du helfen kannst!
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 40.0 39 (39)[1] Nicht unterstützt 24 Nicht unterstützt
add() 44.0 (Ja)[1] ? ? ?
addAll() 46.0 (Ja)[1] ? ? ?
matchAll() 47.0 (Ja)[1] ? ? ?
Require HTTPS for add(), addAll(), and put() 46.0 (Ja)[1] ? ? ?
Feature Android Android Webview Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support Nicht unterstützt Nicht unterstützt 39.0 (39) ? Nicht unterstützt ? Nicht unterstützt 40.0
add() Nicht unterstützt Nicht unterstützt (Ja) ? ? ? ? 44.0
addAll() Nicht unterstützt Nicht unterstützt (Ja) ? ? ? ? 46.0
matchAll() Nicht unterstützt Nicht unterstützt (Ja) ? ? ? ? 46.0
Require HTTPS for add(), addAll(), and put() Nicht unterstützt Nicht unterstützt (Ja) ? ? ? ? 46.0

[1] Service workers (and Push) have been disabled in the Firefox 45 Extended Support Release (ESR.)

See also