Skip to content

Commit

Permalink
reload page when sw installed.
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydzhou committed Jul 19, 2024
1 parent ac470a6 commit a765237
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion public/serviceWorkerRegister.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
window.addEventListener('DOMContentLoaded', function () {
navigator.serviceWorker.register('/serviceWorker.js').then(function (registration) {
console.log('ServiceWorker registration successful with scope: ', registration.scope);
const sw = registration.installing || registration.waiting
if (sw) {
sw.onstatechange = function() {
if (sw.state === 'installed') {
// SW installed. Reload for SW intercept serving SW-enabled page.
console.log('ServiceWorker installed reload page');
window.location.reload();
}
}
}
registration.update().then(res => {
console.log('ServiceWorker registration update: ', res);
});
Expand Down

0 comments on commit a765237

Please sign in to comment.