Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
incognitojam committed Nov 12, 2022
1 parent 86688f0 commit f8f12df
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions src/serviceWorkerRegistration.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,38 +62,48 @@ function registerValidSW(swUrl, config) {
return;
}

console.log('Checking for updates...');
console.log('[ServiceWorkerRegistration] Checking for updates...');
registration.update();
}, 60 * 1000);

if (registration.waiting) {
console.log('[ServiceWorkerRegistration] Update waiting');

// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
}

registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}

console.debug('[ServiceWorkerRegistration] Update found...');
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed' && navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.log(
'New content is available and will be used when all '
+ 'tabs for this page are closed. See https://cra.link/PWA.',
);

// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('Content is cached for offline use.');

// Execute callback
if (config && config.onSuccess) {
config.onSuccess(registration);
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the updated precached content has been fetched,
// but the previous service worker will still serve the older
// content until all client tabs are closed.
console.debug('[ServiceWorkerRegistration] Update ready');

// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// "Content is cached for offline use." message.
console.log('[ServiceWorkerRegistration] Content is cached for offline use');

// Execute callback
if (config && config.onSuccess) {
config.onSuccess(registration);
}
}
}
};
Expand Down Expand Up @@ -128,7 +138,7 @@ function checkValidServiceWorker(swUrl, config) {
}
})
.catch(() => {
console.log('No internet connection found. App is running in offline mode.');
console.log('[ServiceWorkerRegistration] No internet connection found. App is running in offline mode.');
});
}

Expand Down

0 comments on commit f8f12df

Please sign in to comment.