CacheStorage

Secure context:This feature is available only insecure contexts(HTTPS), in some or allsupporting browsers.

Note:This feature is available inWeb Workers.

TheCacheStorageinterface represents the storage forCacheobjects.

The interface:

  • Provides a master directory of all the named caches that can be accessed by aServiceWorkeror other type of worker orwindowscope (you're not limited to only using it with service workers).
  • Maintains a mapping of string names to correspondingCacheobjects.

UseCacheStorage.open()to obtain aCacheinstance.

UseCacheStorage.match()to check if a givenRequestis a key in any of theCacheobjects that theCacheStorageobject tracks.

You can accessCacheStoragethrough theWindow.cachesproperty in windows or through theWorkerGlobalScope.cachesproperty in workers.

Note:CacheStoragealways rejects with aSecurityErroron untrusted origins (i.e. those that aren't using HTTPS, although this definition will likely become more complex in the future.) When testing on Firefox, you can get around this by checking theEnable Service Workers over HTTP (when toolbox is open)option in the Firefox Devtools options/gear menu. Furthermore, becauseCacheStoragerequires file-system access, it may be unavailable in private mode in Firefox.

Note:CacheStorage.match()is a convenience method. Equivalent functionality to match a cache entry can be implemented by returning an array of cache names fromCacheStorage.keys(),opening each cache withCacheStorage.open(),and matching the one you want withCache.match().

Instance methods

CacheStorage.match()

Checks if a givenRequestis a key in any of theCacheobjects that theCacheStorageobject tracks, and returns aPromisethat resolves to that match.

CacheStorage.has()

Returns aPromisethat resolves totrueif aCacheobject matching thecacheNameexists.

CacheStorage.open()

Returns aPromisethat resolves to theCacheobject matching thecacheName(a new cache is created if it doesn't already exist.)

CacheStorage.delete()

Finds theCacheobject matching thecacheName,and if found, deletes theCacheobject and returns aPromisethat resolves totrue.If noCacheobject is found, it resolves tofalse.

CacheStorage.keys()

Returns aPromisethat will resolve with an array containing strings corresponding to all of the namedCacheobjects tracked by theCacheStorage.Use this method to iterate over a list of all theCacheobjects.

Examples

This code snippet is from the MDNsimple service worker example(seesimple service worker running live.) This service worker script waits for aninstallevent to fire, then runswaitUntilto handle the install process for the app. This consists of callingCacheStorage.opento create a new cache, then usingCache.addAllto add a series of assets to it.

In the second code block, we wait for aFetchEventto fire. We construct a custom response like so:

  1. Check whether a match for the request is found in the CacheStorage. If so, serve that.
  2. If not, fetch the request from the network, then also open the cache created in the first block and add a clone of the request to it usingCache.put(cache.put(event.request, response.clone()).)
  3. If this fails (e.g. because the network is down), return a fallback response.

Finally, return whatever the custom response ended up being equal to, usingFetchEvent.respondWith.

js
self.addEventListener( "install", (event) => {
event.waitUntil(
caches
.open( "v1" )
.then((cache) =>
cache.addAll([
"/",
"/index.html",
"/style.css",
"/app.js",
"/image-list.js",
"/star-wars-logo.jpg",
"/gallery/bountyHunters.jpg",
"/gallery/myLittleVader.jpg",
"/gallery/snowTroopers.jpg",
]),
),
);
});

self.addEventListener( "fetch", (event) => {
event.respondWith(
caches.match(event.request).then((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((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((cache) => {
cache.put(event.request, responseClone);
});
return response;
})
.catch(() => caches.match( "/gallery/myLittleVader.jpg" ));
}
}),
);
});

This snippet shows how the API can be used outside of a service worker context, and uses theawaitoperator for much more readable code.

js
// Try to get data from the cache, but fall back to fetching it live.
async function getData() {
const cacheVersion = 1;
const cacheName = `myapp-${cacheVersion}`;
const url = "https://jsonplaceholder.typicode /todos/1";
let cachedData = await getCachedData(cacheName, url);

if (cachedData) {
console.log( "Retrieved cached data" );
return cachedData;
}

console.log( "Fetching fresh data" );

const cacheStorage = await caches.open(cacheName);
await cacheStorage.add(url);
cachedData = await getCachedData(cacheName, url);
await deleteOldCaches(cacheName);

return cachedData;
}

// Get data from the cache.
async function getCachedData(cacheName, url) {
const cacheStorage = await caches.open(cacheName);
const cachedResponse = await cacheStorage.match(url);

if (!cachedResponse ||!cachedResponse.ok) {
return false;
}

return await cachedResponse.json();
}

// Delete any old caches to respect user's disk space.
async function deleteOldCaches(currentCache) {
const keys = await caches.keys();

for (const key of keys) {
const isOurCache = key.startsWith( "myapp-" );
if (currentCache === key ||!isOurCache) {
continue;
}
caches.delete(key);
}
}

try {
const data = await getData();
console.log({ data });
} catch (error) {
console.error({ error });
}

Specifications

Specification
Service Workers
#cachestorage-interface

Browser compatibility

BCD tables only load in the browser

See also