This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.
The match() method of the CacheStorage interface 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 the matching Response.
Cache objects are searched by key insertion order.
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 Cache.match().caches.match(request,{options}).then(function(response) {
//do something with the request
});
a Promise that resolves to the matching Response.
Request you want to match.match operation. The available options are: ignoreSearch: A Boolean that specifies whether the matching process should ignore the query string in the url. 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.Note: If a cacheName is set in the options object and the cache does not exist, the promise will reject (cache.match() just resolves undefined if a matching response is not found.)
This code snippet 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:
CacheStorage using CacheStorage.match. If so, serve that.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() — necessary because put() consumes the response body.caches.match(event.request).then(function(resp) {
return resp || fetch(event.request).then(function(r) {
caches.open('v1').then(function(cache) {
cache.put(event.request, r);
});
return r.clone();
});
}).catch(function() {
return caches.match('/sw-test/gallery/myLittleVader.jpg');
}); | Specification | Status | Comment |
|---|---|---|
| Service Workers The definition of 'CacheStorage' in that specification. | Working Draft | Initial definition. |
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 40.0[1] | 44 (44)[2] | No support | (Yes) | No support |
| All options supported | 54.0 | 41 |
| Feature | Android | Android Webview | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|
| Basic support | No support | No support | 44.0 (44) | (Yes) | (Yes) | (Yes) | 40.0 [1] |
| All options supported | No support | No support | 41 | 54.0 |
ignoreSearch, and cacheName.
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage/match