我們的志工尚未將本文翻譯為 正體中文 (繁體) 版本。加入我們,幫忙翻譯!
您也可以閱讀本文的 English (US) 版本。
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
ServiceWorkeror other type of worker orwindowscope (you’re not limited to only using it with service workers, even though the Service Workers spec defines it). - Maintains a mapping of string names to corresponding
Cacheobjects.
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.
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, 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.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().Methods
CacheStorage.match()- Checks if a given
Requestis a key in any of theCacheobjects that theCacheStorageobject tracks, and returns aPromisethat resolves to that match. CacheStorage.has()- Returns a
Promisethat resolves totrueif aCacheobject matching thecacheNameexists. CacheStorage.open()- Returns a
Promisethat resolves to theCacheobject matching thecacheName(a new cache is created if it doesn't already exist.) CacheStorage.delete()- Finds the
Cacheobject matching thecacheName, and if found, deletes theCacheobject and returns aPromisethat resolves totrue. If noCacheobject is found, it returnsfalse. CacheStorage.keys()- Returns a
Promisethat 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 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.
this.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'
]);
})
);
});
this.addEventListener('fetch', function(event) {
var response;
event.respondWith(caches.match(event.request).catch(function() {
return fetch(event.request);
}).then(function(r) {
response = r;
caches.open('v1').then(function(cache) {
cache.put(event.request, response);
});
return response.clone();
}).catch(function() {
return caches.match('/sw-test/gallery/myLittleVader.jpg');
}));
});
Specifications
| Specification | Status | Comment |
|---|---|---|
| Service Workers The definition of 'CacheStorage' in that specification. |
Working Draft | Initial definition. |
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
CacheStorage | Chrome
Full support
40
| Edge Full support Yes | Firefox
Full support
44
| IE No support No | Opera Full support 27 | Safari Full support 11.1 | WebView Android
Full support
40
| Chrome Android
Full support
40
| Firefox Android Full support 44 | Opera Android Full support 27 | Safari iOS Full support Yes | Samsung Internet Android ? |
delete | Chrome Full support 40 | Edge Full support 16 | Firefox
Full support
44
| IE No support No | Opera Full support 27 | Safari Full support 11.1 | WebView Android Full support 40 | Chrome Android Full support 40 | Firefox Android Full support 44 | Opera Android Full support 27 | Safari iOS Full support Yes | Samsung Internet Android ? |
has | Chrome Full support 40 | Edge Full support 16 | Firefox
Full support
44
| IE No support No | Opera Full support 27 | Safari Full support 11.1 | WebView Android Full support 40 | Chrome Android Full support 40 | Firefox Android Full support 44 | Opera Android Full support 27 | Safari iOS Full support Yes | Samsung Internet Android ? |
keys | Chrome Full support 40 | Edge Full support 16 | Firefox
Full support
44
| IE No support No | Opera Full support 27 | Safari Full support 11.1 | WebView Android Full support 40 | Chrome Android Full support 40 | Firefox Android Full support 44 | Opera Android Full support 27 | Safari iOS Full support Yes | Samsung Internet Android ? |
match | Chrome
Full support
54
| Edge Full support 16 | Firefox
Full support
44
| IE No support No | Opera
Full support
41
| Safari Full support 11.1 | WebView Android
Full support
54
| Chrome Android
Full support
54
| Firefox Android Full support 44 | Opera Android
Full support
41
| Safari iOS Full support Yes | Samsung Internet Android ? |
open | Chrome Full support 40 | Edge Full support 16 | Firefox
Full support
44
| IE No support No | Opera Full support 27 | Safari Full support 11.1 | WebView Android Full support 40 | Chrome Android Full support 40 | Firefox Android Full support 44 | Opera Android Full support 27 | Safari iOS Full support Yes | Samsung Internet Android ? |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- Experimental. Expect behavior to change in the future.
- Experimental. Expect behavior to change in the future.
- See implementation notes.
- See implementation notes.