diff --git a/packages/react-scripts/template-typescript/src/serviceWorker.ts b/packages/react-scripts/template-typescript/src/serviceWorker.ts index c0b13105198..abe6dc0964d 100644 --- a/packages/react-scripts/template-typescript/src/serviceWorker.ts +++ b/packages/react-scripts/template-typescript/src/serviceWorker.ts @@ -22,7 +22,7 @@ const isLocalhost = Boolean( type Config = { onSuccess?: (registration: ServiceWorkerRegistration) => void; - onUpdate?: (registration: ServiceWorkerRegistration) => void; + onUpdate?: (registration: ServiceWorkerRegistration, sw: ServiceWorker) => void; }; export function register(config?: Config) { @@ -66,6 +66,10 @@ function registerValidSW(swUrl: string, config?: Config) { navigator.serviceWorker .register(swUrl) .then(registration => { + //a new service worker has previously finished installing, and is now waiting + if (registration.waiting && registration.active) { + newerSwAvailable(registration.waiting); + } registration.onupdatefound = () => { const installingWorker = registration.installing; if (installingWorker == null) { @@ -74,18 +78,7 @@ function registerValidSW(swUrl: string, config?: Config) { installingWorker.onstatechange = () => { 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.log( - 'New content is available and will be used when all ' + - 'tabs for this page are closed. See http://bit.ly/CRA-PWA.' - ); - - // Execute callback - if (config && config.onUpdate) { - config.onUpdate(registration); - } + newerSwAvailable(installingWorker); } else { // At this point, everything has been precached. // It's the perfect time to display a @@ -100,6 +93,18 @@ function registerValidSW(swUrl: string, config?: Config) { } }; }; + function newerSwAvailable(sw: ServiceWorker){ + // 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 http://bit.ly/CRA2-PWA.' + ); + if (config && config.onUpdate) { + config.onUpdate(registration, sw); + } + } }) .catch(error => { console.error('Error during service worker registration:', error); diff --git a/packages/react-scripts/template/src/serviceWorker.js b/packages/react-scripts/template/src/serviceWorker.js index 2283ff9ced1..6bedf6021ff 100644 --- a/packages/react-scripts/template/src/serviceWorker.js +++ b/packages/react-scripts/template/src/serviceWorker.js @@ -58,6 +58,10 @@ function registerValidSW(swUrl, config) { navigator.serviceWorker .register(swUrl) .then(registration => { + //a new service worker has previously finished installing, and is now waiting + if (registration.waiting && registration.active) { + newerSwAvailable(registration.waiting); + } registration.onupdatefound = () => { const installingWorker = registration.installing; if (installingWorker == null) { @@ -66,18 +70,7 @@ function registerValidSW(swUrl, config) { installingWorker.onstatechange = () => { 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.log( - 'New content is available and will be used when all ' + - 'tabs for this page are closed. See http://bit.ly/CRA-PWA.' - ); - - // Execute callback - if (config && config.onUpdate) { - config.onUpdate(registration); - } + newerSwAvailable(installingWorker); } else { // At this point, everything has been precached. // It's the perfect time to display a @@ -92,6 +85,18 @@ function registerValidSW(swUrl, config) { } }; }; + function newerSwAvailable(sw){ + // 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 http://bit.ly/CRA2-PWA.' + ); + if (config && config.onUpdate) { + config.onUpdate(registration, sw); + } + } }) .catch(error => { console.error('Error during service worker registration:', error);