You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.2 KiB
46 lines
1.2 KiB
(function (window, undefined) { |
|
window.addEventListener('DOMContentLoaded', () => { |
|
|
|
if (!('serviceWorker' in navigator)) { |
|
console.log('service workers not supported 😣') |
|
return |
|
} |
|
|
|
let initialized = false; |
|
const initialize = () => { |
|
initialized = true; |
|
}; |
|
|
|
navigator.serviceWorker.register('serviceworker.js', {scope: "/"}).then( |
|
(reg) => { |
|
if(reg.installing) { |
|
console.log('Service worker installing'); |
|
setTimeout(() => { |
|
window.location = window.location.origin+window.location.pathname; |
|
}, 200); |
|
} else if(reg.waiting) { |
|
console.log('Service worker installed'); |
|
if(!initialized) { |
|
initialize(); |
|
} |
|
} else if(reg.active) { |
|
console.log('Service worker active'); |
|
if(!initialized) { |
|
initialize(); |
|
} |
|
} |
|
}, |
|
err => { |
|
console.error('service worker registration failed! 😱', err) |
|
} |
|
); |
|
|
|
navigator.serviceWorker.addEventListener('message', event => { |
|
if(event.data.log) { |
|
console.log(`serviceworker: ${event.data.log}`); |
|
} |
|
}); |
|
|
|
|
|
}); |
|
})(window); |