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().Syntax
cacheStorage.match(request,{options}).then(function(response) {
//do something with the request
});
Returns
a Promise that resolves to the matching Response. If a matching response to the specified requestis not found, the promise resolves as undefined.
Parameters
- request
- The
Requestyou want to match. - options Optional
- An object whose properties control how matching is done in the
matchoperation. The available options are:ignoreSearch: ABooleanthat specifies whether the matching process should ignore the query string in the url. If set totrue, the?value=barpart ofhttp://foo.com/?value=barwould be ignored when performing a match. It defaults tofalse.ignoreMethod: ABooleanthat, when set totrue, prevents matching operations from validating theRequesthttpmethod (normally onlyGETandHEADare allowed.) It defaults tofalse.ignoreVary: ABooleanthat, when set totrue,tells the matching operation not to performVARYheader matching. In other words, if the URL matches you will get a match regardless of whether theResponseobject has aVARYheader or not. It defaults tofalse.cacheName: ADOMStringthat represents a specific cache to search within.
Examples
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:
- Check whether a match for the request is found in the
CacheStorageusingCacheStorage.match. If so, serve that. - If not, open the
v1cache usingopen(), put the default network request in the cache usingCache.putand return a clone of the default network request usingreturn response.clone()— necessary becauseput()consumes the response body. - If this fails (e.g., because the network is down), return a fallback response.
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');
});
Specifications
| Specification | Status | Comment |
|---|---|---|
| Service Workers The definition of 'CacheStorage' in that specification. |
Working Draft | Initial definition. |
Browser compatibility
| 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 |
- [1] The options parameter only supports
ignoreSearch, andcacheName. - [2] Service workers (and Push) have been disabled in the Firefox 45 & 52 Extended Support Releases (ESR.)