CacheStorage: phương thức open()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This feature is available in Web Workers.
Phương thức open() của giao diện
CacheStorage trả về một Promise phân giải thành
đối tượng Cache khớp với cacheName.
Bạn có thể truy cập CacheStorage thông qua thuộc tính Window.caches trong cửa sổ hoặc thông qua thuộc tính WorkerGlobalScope.caches trong worker.
Cú pháp
open(cacheName)
Tham số
cacheName-
Tên của bộ nhớ đệm bạn muốn mở.
Giá trị trả về
Ví dụ
Ví dụ này lấy từ ví dụ service worker đơn giản của MDN (xem bản chạy trực tiếp của service worker đơn giản).
Ở đây chúng ta chờ một InstallEvent xảy ra, sau đó chạy
waitUntil() để xử lý quá trình cài đặt cho
ứng dụng. Việc này bao gồm gọi CacheStorage.open() để tạo một bộ nhớ đệm mới,
sau đó dùng Cache.addAll() để thêm một loạt tài nguyên vào đó.
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",
]),
),
);
});
Thông số kỹ thuật
| Specification |
|---|
| Service Workers Nightly> # cache-storage-open> |