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
you want to match. This can be a
Request
object or a URL string.
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:
CacheStorage
使用
CacheStorage.match()
. If so, serve that.
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.
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. |
工作草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
match
|
Chrome
54
|
Edge 16 |
Firefox
44
|
IE No |
Opera
41
|
Safari 11.1 |
WebView Android
54
|
Chrome Android
54
|
Firefox Android 44 |
Opera Android
41
|
Safari iOS Yes |
Samsung Internet Android
6.0
|
完整支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。
CacheStorage
缓存
Client
Clients
ExtendableEvent
FetchEvent
InstallEvent
Navigator.serviceWorker
NotificationEvent
PeriodicSyncEvent
PeriodicSyncManager
PeriodicSyncRegistration
ServiceWorker
ServiceWorkerContainer
ServiceWorkerGlobalScope
ServiceWorkerRegistration
SyncEvent
SyncManager
SyncRegistration
WindowClient