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
Request
you want to match. - options Optional
- An object whose properties control how matching is done in the
match
operation. The available options are:ignoreSearch
: ABoolean
that specifies whether the matching process should ignore the query string in the url. If set totrue
, the?value=bar
part ofhttp://foo.com/?value=bar
would be ignored when performing a match. It defaults tofalse
.ignoreMethod
: ABoolean
that, when set totrue
, prevents matching operations from validating theRequest
http
method (normally onlyGET
andHEAD
are allowed.) It defaults tofalse
.ignoreVary
: ABoolean
that, when set totrue,
tells the matching operation not to performVARY
header matching. In other words, if the URL matches you will get a match regardless of whether theResponse
object has aVARY
header or not. It defaults tofalse
.cacheName
: ADOMString
that 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
CacheStorage
usingCacheStorage.match
. If so, serve that. - If not, open the
v1
cache usingopen()
, put the default network request in the cache usingCache.put
and 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.)