CacheStorage.match()

The match() method of the CacheStorage interface checks if a given Request or url string is a key for a stored Response. This method returns a Promise for a Response, or a Promise which resolves to undefined if no match is found.

You can access CacheStorage through the global caches property.

Cache objects are searched in creation order.

Note: caches.match() is a convenience method. Equivalent functionality is to call cache.match() on each cache (in the order returned by caches.keys()) until a Response is returned.

Syntax

caches.match(request, options).then(function(response) {
  // Do something with the response
});

Parameters

request
The Request you want to match. This can be a Request object or a URL string.
options Optional
An object whose properties control how matching is done in the match operation. The available options are:
  • ignoreSearch: A Boolean that specifies whether the matching process should ignore the query string in the url. For example, if set to true, the ?value=bar part of http://foo.com/?value=bar would be ignored when performing a match. It defaults to false.
  • ignoreMethod: A Boolean that, when set to true, prevents matching operations from validating the Request http method (normally only GET and HEAD are allowed.) It defaults to false.
  • ignoreVary: A Boolean that, when set to true, tells the matching operation not to perform VARY header matching. In other words, if the URL matches you will get a match regardless of whether the Response object has a VARY header or not. It defaults to false.
  • cacheName: A DOMString that represents a specific cache to search within.

Return value

a Promise that resolves to the matching Response. If no matching response to the specified request is found, the promise resolves with undefined.

Examples

This example is from the MDN sw-test example (see sw-test running live). Here 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 using CacheStorage.match(). If so, serve that.
  2. If not, open the v1 cache using 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.
self.addEventListener('fetch', function(event) {
  event.respondWith(caches.match(event.request).then(function(response) {
    // caches.match() always resolves
    // but in case of success response will have value
    if (response !== undefined) {
      return response;
    } else {
      return fetch(event.request).then(function (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(function (cache) {
          cache.put(event.request, responseClone);
        });
        return response;
      }).catch(function () {
        return caches.match('/sw-test/gallery/myLittleVader.jpg');
      });
    }
  }));
});

Specifications

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

Browser compatibility

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
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 16Firefox Full support 44
Notes
Full support 44
Notes
Notes Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
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.
Firefox 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 Full support 6.0
Full support 6.0
Partial support 4.0
Notes
Notes The options parameter only supports ignoreSearch, and cacheName.

Legend

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

See also