From 6b70ec371cd5dbb14b268262b9136d34fdb3a9dc Mon Sep 17 00:00:00 2001 From: Piotr Date: Tue, 31 Oct 2017 10:26:28 +0100 Subject: [PATCH 1/2] Provide callbacks in registerServiceWorker --- .../template/src/registerServiceWorker.js | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/react-scripts/template/src/registerServiceWorker.js b/packages/react-scripts/template/src/registerServiceWorker.js index 12542ba2295..2fdddbfb169 100644 --- a/packages/react-scripts/template/src/registerServiceWorker.js +++ b/packages/react-scripts/template/src/registerServiceWorker.js @@ -18,7 +18,7 @@ const isLocalhost = Boolean( ) ); -export default function register() { +export default function register(config) { if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { // The URL constructor is available in all browsers that support SW. const publicUrl = new URL(process.env.PUBLIC_URL, window.location); @@ -34,16 +34,16 @@ export default function register() { if (isLocalhost) { // This is running on localhost. Lets check if a service worker still exists or not. - checkValidServiceWorker(swUrl); + checkValidServiceWorker(swUrl, config); } else { // Is not local host. Just register service worker - registerValidSW(swUrl); + registerValidSW(swUrl, config); } }); } } -function registerValidSW(swUrl) { +function registerValidSW(swUrl, config) { navigator.serviceWorker .register(swUrl) .then(registration => { @@ -57,11 +57,21 @@ function registerValidSW(swUrl) { // It's the perfect time to display a "New content is // available; please refresh." message in your web app. console.log('New content is available; please refresh.'); + + // Execute callback + if (config.handleUpdate) { + config.handleUpdate(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.handleSuccess) { + config.handleSuccess(registration); + } } } }; @@ -72,7 +82,7 @@ function registerValidSW(swUrl) { }); } -function checkValidServiceWorker(swUrl) { +function checkValidServiceWorker(swUrl, config) { // Check if the service worker can be found. If it can't reload the page. fetch(swUrl) .then(response => { @@ -89,7 +99,7 @@ function checkValidServiceWorker(swUrl) { }); } else { // Service worker found. Proceed as normal. - registerValidSW(swUrl); + registerValidSW(swUrl, config); } }) .catch(() => { From d7a7b3f0c74ecce53f7defd5abc6a76f8577d980 Mon Sep 17 00:00:00 2001 From: Piotr Date: Tue, 31 Oct 2017 10:33:37 +0100 Subject: [PATCH 2/2] change callback names from handleX to onX --- .../react-scripts/template/src/registerServiceWorker.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/react-scripts/template/src/registerServiceWorker.js b/packages/react-scripts/template/src/registerServiceWorker.js index 2fdddbfb169..356303a3c61 100644 --- a/packages/react-scripts/template/src/registerServiceWorker.js +++ b/packages/react-scripts/template/src/registerServiceWorker.js @@ -59,8 +59,8 @@ function registerValidSW(swUrl, config) { console.log('New content is available; please refresh.'); // Execute callback - if (config.handleUpdate) { - config.handleUpdate(registration); + if (config.onUpdate) { + config.onUpdate(registration); } } else { // At this point, everything has been precached. @@ -69,8 +69,8 @@ function registerValidSW(swUrl, config) { console.log('Content is cached for offline use.'); // Execute callback - if (config.handleSuccess) { - config.handleSuccess(registration); + if (config.onSuccess) { + config.onSuccess(registration); } } }