CacheStorage: open() 方法
注意:此功能在 Web Workers 中可用。
CacheStorage 接口的 open() 方法会返回一个 Promise,该 Promise 解析为与 cacheName 匹配的 Cache 对象。
您可以通过窗口中的 Window.caches 属性或在 worker 中的 WorkerGlobalScope.caches 属性来访问 CacheStorage。
语法
js
open(cacheName)
参数
cacheName-
要打开的缓存的名称。
返回值
示例
此示例来自 MDN 的 简单 Service Worker 示例(请参阅 正在运行的简单 Service Worker)。在这里,我们等待 InstallEvent 触发,然后运行 waitUntil() 来处理应用程序的安装过程。这包括调用 CacheStorage.open() 来创建一个新的缓存,然后使用 Cache.addAll() 将一系列资源添加到其中。
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",
]),
),
);
});
规范
| 规范 |
|---|
| Service Workers # cache-storage-open |
浏览器兼容性
加载中…