Cache.match() - Web APIs 编辑

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
});

Parameters

request
The Request for which you are attempting to find responses in the Cache. This can be a Request object or a URL.
options Optional
An object that sets options for the match operation. The available options are:
  • ignoreSearch: A Boolean that specifies whether to ignore the query string in the URL.  For example, if set to true the ?value=bar part of http://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 — i.e. if the URL matches you will get a match regardless of whether the Response object has a VARY header. It defaults to false.

Return value

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 that rather than resolving with an array of all matching responses, it resolves with the first matching response only (that is, response[0]).

Examples

This example is taken from the custom offline page example (live demo). It 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, 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

SpecificationStatusComment
Service Workers
The definition of 'Cache match' in that specification.
Working DraftInitial definition.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:57 次

字数:6840

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文