From d546fef71ce95b561017bad466ee0294987a5383 Mon Sep 17 00:00:00 2001 From: Jeremy Kahn Date: Mon, 11 Dec 2023 19:27:19 -0600 Subject: [PATCH] fix(service-worker): Install service worker correctly (#222) * fix(service-worker): register service worker immediately --- src/serviceWorkerRegistration.js | 34 +++++++++++++++----------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/serviceWorkerRegistration.js b/src/serviceWorkerRegistration.js index 6683b45..ce516d8 100644 --- a/src/serviceWorkerRegistration.js +++ b/src/serviceWorkerRegistration.js @@ -31,26 +31,24 @@ export function register(config) { return } - window.addEventListener('load', () => { - const swUrl = `${process.env.PUBLIC_URL}/service-worker.js` + const swUrl = `${process.env.PUBLIC_URL}/service-worker.js` - if (isLocalhost) { - // This is running on localhost. Let's check if a service worker still exists or not. - checkValidServiceWorker(swUrl, config) + if (isLocalhost) { + // This is running on localhost. Let's check if a service worker still exists or not. + checkValidServiceWorker(swUrl, config) - // Add some additional logging to localhost, pointing developers to the - // service worker/PWA documentation. - navigator.serviceWorker.ready.then(() => { - console.log( - 'This web app is being served cache-first by a service ' + - 'worker. To learn more, visit https://cra.link/PWA' - ) - }) - } else { - // Is not localhost. Just register service worker - registerValidSW(swUrl, config) - } - }) + // Add some additional logging to localhost, pointing developers to the + // service worker/PWA documentation. + navigator.serviceWorker.ready.then(() => { + console.log( + 'This web app is being served cache-first by a service ' + + 'worker. To learn more, visit https://cra.link/PWA' + ) + }) + } else { + // Is not localhost. Just register service worker + registerValidSW(swUrl, config) + } } }