【发布时间】:2021-11-08 06:41:30
【问题描述】:
这里是一个只缓存应用基本启动文件的 service worker,
self.addEventListener("fetch", event => {
event.respondWith( caches.match(event.request)
.then( cachedResponse => {
return cachedResponse || fetch(event.request);
} )
);
});
const cacheName0 = "bare-bones-of-the-app";
const mainResourcesToPrecache = [
"index.html",
"app.js",
"style.css"
];
self.addEventListener("install", event => {
event.waitUntil(
caches.open(cacheName0).then(cache => {
return cache.addAll(mainResourcesToPrecache);
})
);
});
到目前为止一切顺利。 现在假设这个 PWA 是一款游戏,用户在完成一个又一个关卡时会取得进步。假设我们有,
const cacheName1 = "level-1";
const resourcesForLevel1 = [
"somepicture.jpg",
"someartwork.png",
"somesound.mp3"
];
const cacheName2 = "level-2";
const resourcesForLevel2 = [
"anotherpicture.jpg",
"anotherartwork.png",
"anothersound.mp3"
];
// etc
在这种情况下,我们如何通过在主线程中触发的函数唤醒服务工作者,从而使浏览器在用户到达应用程序中的某些点时提前获取文件?
【问题讨论】:
标签: javascript progressive-web-apps service-worker