match() 方法在 CacheStorage interface checks if a given Request or url string is a key for a stored 响应 . This method returns a Promise 对于 响应 ,或 Promise which resolves to undefined if no match is found.

You can access CacheStorage through the global caches 特性。

缓存 objects are searched in creation order.

注意 : caches.match() is a convenience method. Equivalent functionality is to call cache.match() on each cache (in the order returned by caches.keys() ) until a 响应 被返回。

句法

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

参数

request
Request you want to match.  This can be a Request object or a URL string.
选项 可选
An object whose properties control how matching is done in the match operation. The available options are:
  • ignoreSearch : A 布尔 that specifies whether the matching process should ignore the query string in the url.  For example, if set to true ?value=bar 部分在 http://foo.com/?value=bar would be ignored when performing a match. It defaults to false .
  • ignoreMethod : A 布尔 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 布尔 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 响应 对象拥有 VARY header or not. It defaults to false .
  • cacheName : A DOMString that represents a specific cache to search within.

返回值

a Promise that resolves to the matching 响应 . If no matching response to the specified request is found, the promise resolves with undefined .

范例

This example is from the MDN sw-test example (见 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 使用 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() . The last is necessary because put() consumes the response body.
  3. If this fails (e.g., because the network is down), return a fallback response.
self.addEventListener('fetch', function(event) {
  event.respondWith(caches.match(event.request).then(function(response) {
    // caches.match() always resolves
    // but in case of success response will have value
    if (response !== undefined) {
      return response;
    } else {
      return fetch(event.request).then(function (response) {
        // response may be used only once
        // we need to save clone to put one copy in cache
        // and serve second one
        let responseClone = response.clone();
        caches.open('v1').then(function (cache) {
          cache.put(event.request, responseClone);
        });
        return response;
      }).catch(function () {
        return caches.match('/sw-test/gallery/myLittleVader.jpg');
      });
    }
  }));
});
					

规范

规范 状态 注释
服务工作者
The definition of 'CacheStorage: match' in that specification.
工作草案 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
match Chrome 54
54
部分支持 40
The options parameter only supports ignoreSearch ,和 cacheName .
Edge 16 Firefox 44
44
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
IE No Opera 41
41
部分支持 27
The options parameter only supports ignoreSearch ,和 cacheName .
Safari 11.1 WebView Android 54
54
部分支持 40
The options parameter only supports ignoreSearch ,和 cacheName .
Chrome Android 54
54
部分支持 40
The options parameter only supports ignoreSearch ,和 cacheName .
Firefox Android 44 Opera Android 41
41
部分支持 27
The options parameter only supports ignoreSearch ,和 cacheName .
Safari iOS Yes Samsung Internet Android 6.0
6.0
部分支持 4.0
The options parameter only supports ignoreSearch ,和 cacheName .

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

另请参阅

元数据

  • 最后修改:
  1. 服务工作者 API
  2. CacheStorage
  3. 方法
    1. delete()
    2. has()
    3. keys()
    4. match()
    5. open()
  4. Related pages for Service Workers API
    1. 缓存
    2. Client
    3. Clients
    4. ExtendableEvent
    5. FetchEvent
    6. InstallEvent
    7. Navigator.serviceWorker
    8. NotificationEvent
    9. PeriodicSyncEvent
    10. PeriodicSyncManager
    11. PeriodicSyncRegistration
    12. ServiceWorker
    13. ServiceWorkerContainer
    14. ServiceWorkerGlobalScope
    15. ServiceWorkerRegistration
    16. SyncEvent
    17. SyncManager
    18. SyncRegistration
    19. WindowClient

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1