CacheStorage.open()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2018年4月.
备注: 此特性在 Web Worker 中可用。
CacheStorage 接口的 open() 方法返回一个兑现为匹配 cacheName 的 Cache 对象的 Promise。
你可以通过窗口的 Window.caches 属性或 worker 的 WorkerGlobalScope.caches 属性访问 CacheStorage。
语法
js
open(cacheName)
参数
- cacheName
-
要打开的缓存对象的名称。
返回值
示例
此示例来自于 MDN service worker 简单示例(请参阅在线的 service worker 简单示例)。这里,等待 FetchEvent 事件触发,然后运行 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",
]),
),
);
});
规范
| Specification |
|---|
| Service Workers> # cache-storage-open> |
浏览器兼容性
Loading…