The put()
method of the Cache
interface allows key/value pairs to be added to the current Cache
object.
Often, you will just want to fetch()
one or more requests, then add the result straight to your cache. In such cases you are better off using Cache.add()
/Cache.addAll()
, as they are shorthand functions for one or more of these operations.
js
fetch(url).then((response) => { if (!response.ok) { throw new TypeError("Bad response status"); } return cache.put(url, response); });
Note: put()
will overwrite any key/value pair previously stored in the cache that matches the request.
Note: Cache.add
/Cache.addAll
do not cache responses with Response.status
values that are not in the 200 range, whereas Cache.put
lets you store any request/response pair. As a result, Cache.add
/Cache.addAll
can't be used to store opaque responses, whereas Cache.put
can.