CacheStorage
The CacheStorage
interface represents the storage for Cache
objects.
The interface:
- Provides a master directory of all the named caches that can be accessed by a
ServiceWorker
or other type of worker orwindow
scope (you’re not limited to only using it with service workers).Note: Chrome and Safari only expose `CacheStorage` to the windowed context over HTTPS.
caches
will be undefined unless an SSL certificate is configured. - Maintains a mapping of string names to corresponding
Cache
objects.
Use CacheStorage.open()
to obtain a Cache
instance.
Use CacheStorage.match()
to check if a given Request
is a key in any of the Cache
objects that the CacheStorage
object tracks.
You can access CacheStorage
through the global caches
property.
Note: CacheStorage
always rejects with a SecurityError
on 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 the Enable Service Workers over HTTP (when toolbox is open) option in the Firefox Devtools options/gear menu. Furthermore, because CacheStorage
requires 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 from CacheStorage.keys()
, opening each cache with CacheStorage.open()
, and matching the one you want with Cache.match()
.
Note: This feature is available in Web Workers
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Methods
CacheStorage.match()
-
Checks if a given
Request
is a key in any of theCache
objects that theCacheStorage
object tracks, and returns aPromise
that resolves to that match. CacheStorage.has()
-
Returns a
Promise
that resolves totrue
if aCache
object matching thecacheName
exists. CacheStorage.open()
-
Returns a
Promise
that resolves to theCache
object matching thecacheName
(a new cache is created if it doesn't already exist.) CacheStorage.delete()
-
Finds the
Cache
object matching thecacheName
, and if found, deletes theCache
object and returns aPromise
that resolves totrue
. If noCache
object is found, it resolves tofalse
. CacheStorage.keys()
-
Returns a
Promise
that will resolve with an array containing strings corresponding to all of the namedCache
objects tracked by theCacheStorage
. Use this method to iterate over a list of all theCache
objects.
Examples
This code snippet is from the MDN sw-test example (see sw-test running live.) This service worker script waits for an InstallEvent
to fire, then runs waitUntil
to handle the install process for the app. This consists of calling CacheStorage.open
to create a new cache, then using Cache.addAll
to add a series of assets to it.
In the second code block, we wait for a FetchEvent
to fire. We construct a custom response like so:
- Check whether a match for the request is found in the CacheStorage. If so, serve that.
- 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 using
Cache.put
(cache.put(event.request, response.clone())
.) - 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, using FetchEvent.respondWith
.
self.addEventListener('install', function(event) { event.waitUntil( caches.open('v1').then(function(cache) { return cache.addAll([ '/sw-test/', '/sw-test/index.html', '/sw-test/style.css', '/sw-test/app.js', '/sw-test/image-list.js', '/sw-test/star-wars-logo.jpg', '/sw-test/gallery/bountyHunters.jpg', '/sw-test/gallery/myLittleVader.jpg', '/sw-test/gallery/snowTroopers.jpg' ]); }) ); }); 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'); }); } })); });
This snippet shows how the API can be used outside of a service worker context, and uses the await
operator for much more readable code.
// 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.com/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 = 'myapp-' === key.substr( 0, 6 ); 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 1 # cachestorage-interface |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
CacheStorage |
40
Before version 43, only service workers are supported. From version 43, all worker types and the main thread are supported.
|
16 |
44
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
|
No |
27
Before version 30, only service workers are supported. From version 30, all worker types and the main thread are supported.
|
11.1 |
40
Before version 43, only service workers are supported. From version 43, all worker types and the main thread are supported.
|
40
Before version 43, only service workers are supported. From version 43, all worker types and the main thread are supported
|
44 |
27
Before version 30, only service workers are supported. From version 30, all worker types and the main thread are supported.
|
11.3 |
4.0 |
delete |
40 |
16 |
44
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
|
No |
27 |
11.1 |
40 |
40 |
44 |
27 |
Yes |
4.0 |
has |
40 |
16 |
44
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
|
No |
27 |
11.1 |
40 |
40 |
44 |
27 |
Yes |
4.0 |
keys |
40 |
16 |
44
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
|
No |
27 |
11.1 |
40 |
40 |
44 |
27 |
Yes |
4.0 |
match |
54
40
The options parameter only supports
ignoreSearch , and cacheName . |
16 |
44
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
|
No |
41
27
The options parameter only supports
ignoreSearch , and cacheName . |
11.1 |
54
40
The options parameter only supports
ignoreSearch , and cacheName . |
54
40
The options parameter only supports
ignoreSearch , and cacheName . |
44 |
41
27
The options parameter only supports
ignoreSearch , and cacheName . |
Yes |
6.0
4.0
The options parameter only supports
ignoreSearch , and cacheName . |
open |
40 |
16 |
44
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers and the Push API.
|
No |
27 |
11.1 |
40 |
40 |
44 |
27 |
Yes |
4.0 |
secure_context_required |
65 |
≤79 |
44 |
No |
52 |
11.1 |
65 |
65 |
44 |
47 |
11.3 |
9.0 |
worker_support |
40
Before version 43, only service workers are supported. From version 43, all worker types and the main thread are supported.
|
≤18 |
44
Extended Support Releases (ESR) before Firefox 78 ESR do not support service workers.
|
No |
27
Before version 30, only service workers are supported. From version 30, all worker types and the main thread are supported.
|
11.1 |
40
Before version 43, only service workers are supported. From version 43, all worker types and the main thread are supported.
|
40
Before version 43, only service workers are supported. From version 43, all worker types and the main thread are supported.
|
44 |
27
Before version 30, only service workers are supported. From version 30, all worker types and the main thread are supported.
|
11.3 |
4.0 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage