【发布时间】:2021-02-23 09:29:23
【问题描述】:
我有一个渐进式 Web 应用程序,其中服务工作者的配置如下所示。我遵循预缓存方法。每个文件将首先被缓存,请求将从缓存中提供。如果本地缓存中没有匹配项,则通过网络提供请求。如果一切都失败了,则会显示一个离线/错误页面。一切正常。但我坚持更新 index.html 文件。
const pb_cache = "cv1";
const assets = [
"./manifest.json",
"./index.html",
"./offline.html",
]
self.addEventListener("install", installEvent => {
installEvent.waitUntil(
caches.open(pb_cache)
.then((cache) => {
return cache.addAll(assets)
.then(() => {
return self.skipWaiting(); //To forces the waiting service worker to become the active service worker
})
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
return response;
}
return fetch(event.request).then(function(response) {
if (response.status === 404) {
return caches.match('/offline.html');
}
return response
});
}).catch(function() {
return caches.match('/offline.html');
})
);
});
场景 我已将网络应用程序安装到我的安卓手机上。一切都被缓存并且工作正常。我需要对 index.html 文件进行更改。所以我在文件中添加了一些调整并更新了网站。但是由于安装在android中的web app服务于本地缓存,所以网站的变化并没有体现出来。所以我需要一种机制来检查更新。我应该检查什么参数或什么东西来更新?我已经阅读了许多与此相关的文档,但我无法掌握其中的内容。
我知道的一件事是,我必须检查服务工作者中某处的更新,并在网络中找到时将其添加到缓存中。我不知道要检查哪个事件或什么条件。
【问题讨论】:
标签: progressive-web-apps service-worker service-worker-events