我正在尝试首次配置Service Worker,但我遇到两个问题。
我该如何解决?
这是我的html页面中的代码
if ('serviceWorker' in navigator) { window.addEventListener('load', () => {
navigator.serviceWorker
.register('/serviceworker-ver-6.js', { updateViaCache: 'none'})
.then(reg=>console.log('SW registrato'))
.catch(err => console.log('Sw errore'))
}) }
服务人员守则
self.addEventListener('install', (e) => {
console.log('Service Worker: Installed');
e.waitUntil(
caches
.open(cacheName)
.then(cache => {
console.log('Service worker: Caching Files' );
cache.addAll(cacheAssets);
})
.then( () => self.skipWaiting())
);
});
self.addEventListener('fetch', function(e) {
e.respondWith(fetch(e.request).then(function(response) {
cache.put(e.request, response.clone());
return response;
}).catch(function() {
return caches.match(e.request);
})
);
});
谢谢!