Skip to content

Commit

Permalink
Merge pull request #35 from eddieferrer/service_worker_change
Browse files Browse the repository at this point in the history
Update service worker to not start registering until window is loaded…
  • Loading branch information
eddieferrer authored Jul 16, 2020
2 parents 3c6cced + c93ea77 commit b02ac8b
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions frontend/src/registerServiceWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,41 @@
import { register } from 'register-service-worker';

if (process.env.NODE_ENV === 'production') {
register(`${process.env.BASE_URL}service-worker.js`, {
ready() {
console.log('Service worker is active.');
},
registered(registration) {
console.log('Service worker has been registered.');
// Wait until window has loaded to register service worker
// to optimize load time for first visits
window.addEventListener('load', () => {
register(`${process.env.BASE_URL}service-worker.js`, {
ready() {
console.log('Service worker is active.');
},
registered(registration) {
console.log('Service worker has been registered.');

// Routinely check for app updates by testing for a new service worker.
setInterval(() => {
registration.update();
}, 1000 * 60 * 60); // hourly checks
},
cached() {
console.log('Content has been cached for offline use.');
},
updatefound() {
console.log('New content is downloading.');
},
updated(registration) {
console.log('New content is available; please refresh.');
// Routinely check for app updates by testing for a new service worker.
setInterval(() => {
registration.update();
}, 1000 * 60 * 60); // hourly checks
},
cached() {
console.log('Content has been cached for offline use.');
},
updatefound() {
console.log('New content is downloading.');
},
updated(registration) {
console.log('New content is available; please refresh.');

// Add a custom event and dispatch it.
// Used to display of a 'refresh' banner following a service worker update.
// Set the event payload to the service worker registration object.
document.dispatchEvent(new CustomEvent('swUpdated', { detail: registration.waiting }));
},
offline() {
console.log('No internet connection found. App is running in offline mode.');
},
error(error) {
console.error('Error during service worker registration:', error);
},
// Add a custom event and dispatch it.
// Used to display of a 'refresh' banner following a service worker update.
// Set the event payload to the service worker registration object.
document.dispatchEvent(new CustomEvent('swUpdated', { detail: registration.waiting }));
},
offline() {
console.log('No internet connection found. App is running in offline mode.');
},
error(error) {
console.error('Error during service worker registration:', error);
},
});
});
}

0 comments on commit b02ac8b

Please sign in to comment.