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 Cache interface returns a Promise that resolves to the Response associated with the first matching request in the Cache object. If no match is found, the Promise resolves to undefined.
Syntax
cache.match(request,{options}).then(function(response) {
//do something with the response
});
Returns
A Promise that resolves to the first Response that matches the request or to undefined if no match is found.
Note: Cache.match() is basically identical to Cache.matchAll(), except Cache.match() resolves with response[0] (the first matching response) instead of response[] (all matching response in an array).
Parameters
- request
- The
Requestyou are attempting to find in theCache. - options Optional
- An object that sets options for the
matchoperation. The available options are:ignoreSearch: ABooleanthat specifies whether to ignore the query string in the url. For example, if set totruethe?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 totruetells the matching operation not to performVARYheader matching — i.e. if the URL matches you will get a match regardless of whether theResponseobject has aVARYheader. It defaults tofalse.cacheName: ADOMStringthat represents a specific cache to search within. Note that this option is ignored byCache.match().
Examples
This example is taken from the custom offline page example (live demo).
The following example uses a cache to supply selected data when a request fails. A catch() clause is triggered when the call to fetch() throws an exception. Inside the catch() clause, match() is used to return the correct response.
In this example, we decided that only HTML documents retrieved with the GET HTTP verb will be cached. If our if() condition is false, then this fetch handler won't intercept the request. If there are any other fetch handlers registered, they will get a chance to call event.respondWith(). If no fetch handlers call event.respondWith(), the request will be handled by the browser as if there were no service worker involvement. If fetch() returns a valid HTTP response with an response code in the 4xx or 5xx range, the catch() will NOT be called.
self.addEventListener('fetch', function(event) {
// We only want to call event.respondWith() if this is a GET request for an HTML document.
if (event.request.method === 'GET' &&
event.request.headers.get('accept').indexOf('text/html') !== -1) {
console.log('Handling fetch event for', event.request.url);
event.respondWith(
fetch(event.request).catch(function(e) {
console.error('Fetch failed; returning offline page instead.', e);
return caches.open(OFFLINE_CACHE).then(function(cache) {
return cache.match(OFFLINE_URL);
});
})
);
}
});
Specifications
| Specification | Status | Comment |
|---|---|---|
| Service Workers The definition of 'Cache' in that specification. |
Working Draft | Initial definition. |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | 40.0 [1] | 39 (39)[2] | No support | 24 | No support |
| All options supported | 54.0 | 41 |
| Feature | Android | Android Webview | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
|---|---|---|---|---|---|---|---|---|
| Basic support | No support | No support | 39.0 (39) | ? | No support | (Yes) | No support | 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.)