这是在其中注册服务工作者文件的代码
window.addEventListener('load', async e => {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('sw.js', {
scope: '.' // <--- THIS BIT IS REQUIRED
}).then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}, function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
}
});
sw.js代码
const staticAssets = [
'./',
'./app.js',
'./assets/css/style.css',
'./assets/images/banner1.jpg',
'./assets/images/banner2.jpg',
'./assets/images/banner3.jpg'
];
self.addEventListener('install', async event => {
const cache = await caches.open('game-static');
cache.addAll(staticAssets);
//console.log('install');
});
self.addEventListener('fetch', event => {
// it can be empty if you just want to get rid of that error
});
将服务器转换为Https后,正在控制台中记录此错误:
ServiceWorker注册失败:DOMException