The Cache interface provides a storage mechanism for Request
/ Response
object pairs that are cahced, for example as part of the ServiceWorker
lifecycle. Note that the Cache Interface is exposed to windowed scopes as well as workers. You don’t have to use it in conjunction with service workers, even though it is defined in the service worker spec.
An orgin can have multiple Cache Objects
. You are responsible for implementing how your script handles Cache Update
. Items in a Cache do not get updated unless explictly requested – they don’t expires unless deleted.
Use CacheStorage.open(cacheName)
to open a specific named cache object and then call any of the cache methods to maintain the cache.
The caching API doesn’t honor HTTP caching headers.
Methods
Cache.match(request, options)
Returns a
Promise
that resolves to the response associated with the first matching request in the Cache Object.
1 | cache.match(request).then(response => { |
Cache.matchAll(request, options)
Returns a
Promise
that resolves to an array of all matching requests in the Cache Object.Cache.add(request)
Takes a URL, retrieves it and adds the resulting response object to the given cache. This is functionally equivalent to calling
fetch()
, then usingCache.put()
to add the results to the cache.Cache.addAll(requests)
Takes an array of URLs, retrieves them, and adds the resulting response objects to the given cache.
Cache.put(request, response)
Takes both a request and its response and adds it to the given cache. // namely cache the Request/Response pair.
Cache.delete(request, options)
Finds the Cache entry whose key is the request, and if found, deletes the Cache entry and returns a
Promsie
that resolves totrue
. Otherwise resolve tofalse
.Cache.keys(request, options)
Returns a
Promise
that resolves to an array ofCache Key
.