Javascript PWA:服务工作者已更新但未缓存文件

Javascript PWA:服务工作者已更新但未缓存文件,javascript,caching,pwa,Javascript,Caching,Pwa,我目前正试图从头开始构建一个PWA。我在处理更新时遇到一些问题 基于,我编写了一个代码,在新版本的服务人员可用时显示一个按钮。单击该按钮时,将安装新的工作程序,并且shell文件存储在缓存中 为了测试这个特性,我构建了一个最小的示例,其中只有一个index.html和一个service worker.js文件 var cache_version='v2'; var cache_name='test pwa-'+cache_版本; var shell_文件=[ “./index.html” ];

我目前正试图从头开始构建一个PWA。我在处理更新时遇到一些问题

基于,我编写了一个代码,在新版本的服务人员可用时显示一个按钮。单击该按钮时,将安装新的工作程序,并且shell文件存储在缓存中

为了测试这个特性,我构建了一个最小的示例,其中只有一个
index.html
和一个
service worker.js
文件

var cache_version='v2';
var cache_name='test pwa-'+cache_版本;
var shell_文件=[
“./index.html”
];
self.addEventListener('message',evt=>{
if(evt.data.action=='skipWaiting')
自我skipWaiting();
});
self.addEventListener('install',evt=>{
waitill(caches.open(cache_name).then(cache=>{
返回cache.addAll(shell_文件);
}));
});
self.addEventListener('activate',evt=>{
evt.waitill(caches.keys().then(key_list=>{
返回Promise.all(key_list.map)(key=>{
if(cache_name.indexOf(key)=-1)
返回缓存。删除(键);
}));
}));
});
self.addEventListener('fetch',evt=>{
evt.响应(
caches.match(evt.request)。然后(cache\u response=>{
返回cache|response|fetch(evt.request)。然后(response=>{
返回缓存。打开(缓存名称)。然后(缓存=>{
cache.put(evt.request,response.clone());
返回响应;
});
});
})
);
});

PWA测试
你好,世界!
新的更新

if(导航器中的“serviceWorker”){ 新工人; var=false; navigator.serviceWorker.addEventListener('controllerchange',()=>{ 如果(刷新) 返回; window.location.reload(); 刷新=真; }); navigator.serviceWorker.register('service-worker.js')。然后(registration=>{ registration.addEventListener('updatefound',()=>{ 新工人=注册。安装; new_worker.addEventListener('statechange',()=>{ if(new_worker.state=='installed'&&navigator.servicewer.controller){ var通知=document.querySelector(“#刷新”); notification.querySelector('button')。addEventListener('click',()=>{ new_worker.postMessage({action:'skipWaiting'}); }); notification.style.display='block'; } }); }); }).catch(错误=>{ console.log(“注册过程中出现了一些错误”); }); }
多亏了C14L的评论,我发现问题来自HTTP缓存。为了进行调试,我们可以从浏览器的工具中禁用缓存。对于生产,我们可以绕过HTTP缓存,也可以使用
.htaccess
文件控制它。我为感兴趣的人找到了一些有用的信息。

如果删除旧的缓存文件夹,将再次获取
index.html
文件。也许Firefos的浏览器缓存会妨碍你?您是否尝试过“网络”面板中的“禁用缓存”选项?没有想到这一点,谢谢您的想法。它似乎起作用了。然而,我现在想知道如何使它为一个随机用户工作,而这个用户不必考虑禁用他们的缓存。但是,再次感谢你!