Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

CacheStorage.match()

这篇文章需要技术复核。如何帮忙。

我们的志愿者还没有将这篇文章翻译为 中文 (简体)加入我们帮助完成翻译!

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.

Note: 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

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

Returns

a Promise that resolves to the matching Response.

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: 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 https://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.)

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:

  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() — necessary because put() consumes the response body.
  3. 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

See also

文档标签和贡献者

 此页面的贡献者: jpmedley, ebidel, chrisdavidmills, kscarfone
 最后编辑者: jpmedley,