open()
方法在
CacheStorage
interface returns a
Promise
that resolves to the
缓存
object matching the
cacheName
.
You can access
CacheStorage
through the global
caches
特性。
注意
: If the specified
缓存
does not exist, a new cache is created with that
cacheName
和
Promise
that resolves to this new
缓存
object is returned.
caches.open(cacheName).then(function(cache) {
// Do something with your cache
});
The name of the cache you want to open.
A
Promise
that resolves to the requested
缓存
对象。
This example is from the MDN
sw-test example
(见
sw-test running live
). Here we wait 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.
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'
]);
})
);
| 规范 | 状态 | 注释 |
|---|---|---|
|
服务工作者
The definition of 'CacheStorage: open' in that specification. |
工作草案 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
open
|
Chrome 40 | Edge 16 |
Firefox
44
|
IE No | Opera 27 | Safari 11.1 | WebView Android 40 | Chrome Android 40 | Firefox Android 44 | Opera Android 27 | Safari iOS Yes | Samsung Internet Android 4.0 |
完整支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。
CacheStorage
缓存
Client
Clients
ExtendableEvent
FetchEvent
InstallEvent
Navigator.serviceWorker
NotificationEvent
PeriodicSyncEvent
PeriodicSyncManager
PeriodicSyncRegistration
ServiceWorker
ServiceWorkerContainer
ServiceWorkerGlobalScope
ServiceWorkerRegistration
SyncEvent
SyncManager
SyncRegistration
WindowClient