Cache: put() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Web Workers.

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.

Syntax

js
put(request, response)

Parameters

request

The Request object or URL that you want to add to the cache.

response

The Response you want to match up to the request.

Return value

A Promise that resolves with undefined.

Exceptions

TypeError

Returned if the URL scheme is not http or https.

Examples

This example is from the MDN simple-service-worker example (see simple-service-worker 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 using 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(). Clone is needed because put() consumes the response body.
  3. If this fails (e.g., because the network is down), return a fallback response.
js
let response;
const cachedResponse = caches
  .match(event.request)
  .then((r) => (r !== undefined ? r : fetch(event.request)))
  .then((r) => {
    response = r;
    caches.open("v1").then((cache) => cache.put(event.request, response));
    return response.clone();
  })
  .catch(() => caches.match("/gallery/myLittleVader.jpg"));

Specifications

Specification
Service Workers Nightly
# cache-put

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
put
Chrome – Full support
Chrome 43 (Release date: 2015-05-19)
footnote
footnote Requires HTTPS from version 46.
Edge – Full support
Edge 16 (Release date: 2017-10-17)
footnote Full support
Firefox – Full support
Firefox 41 (Release date: 2015-09-22)
footnote Full support
Opera – Full support
Opera 30 (Release date: 2015-06-09)
footnote
footnote Requires HTTPS from version 33.
Safari – Full support
Safari 11.1 (Release date: 2018-04-12)
footnote Full support
Chrome Android – Full support
Chrome Android 43 (Release date: 2015-05-27)
footnote
footnote Requires HTTPS from version 46.
Firefox for Android – Full support
Firefox for Android 41 (Release date: 2015-09-22)
footnote Full support
Opera Android – Full support
Opera Android 30 (Release date: 2015-06-10)
footnote
footnote Requires HTTPS from version 33.
Safari on iOS – Full support
Safari on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Samsung Internet – Full support
Samsung Internet 4 (Release date: 2016-03-11)
footnote
footnote Requires HTTPS from Samsung Internet 5.0.
WebView Android – Full support
WebView Android 43 (Release date: 2015-05-27)
footnote
footnote Requires HTTPS from version 46.
WebView on iOS – Full support
WebView on iOS 11.3 (Release date: 2018-03-29)
footnote Full support
Deno – Full support
Deno 1.26 (Release date: 2022-09-29)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.

See also