serviceWorker.js
1 // This optional code is used to register a service worker. 2 // register() is not called by default. 3 4 // This lets the app load faster on subsequent visits in production, and gives 5 // it offline capabilities. However, it also means that developers (and users) 6 // will only see deployed updates on subsequent visits to a page, after all the 7 // existing tabs open on the page have been closed, since previously cached 8 // resources are updated in the background. 9 10 // To learn more about the benefits of this model and instructions on how to 11 // opt-in, read https://bit.ly/CRA-PWA 12 13 const isLocalhost = Boolean( 14 window.location.hostname === "localhost" || 15 // [::1] is the IPv6 localhost address. 16 window.location.hostname === "[::1]" || 17 // 127.0.0.1/8 is considered localhost for IPv4. 18 window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/) 19 ); 20 21 export function register(config) { 22 if (process.env.NODE_ENV === "production" && "serviceWorker" in navigator) { 23 // The URL constructor is available in all browsers that support SW. 24 const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 25 if (publicUrl.origin !== window.location.origin) { 26 // Our service worker won't work if PUBLIC_URL is on a different origin 27 // from what our page is served on. This might happen if a CDN is used to 28 // serve assets; see https://github.com/facebook/create-react-app/issues/2374 29 return; 30 } 31 32 window.addEventListener("load", () => { 33 const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 34 35 if (isLocalhost) { 36 // This is running on localhost. Let's check if a service worker still exists or not. 37 checkValidServiceWorker(swUrl, config); 38 39 // Add some additional logging to localhost, pointing developers to the 40 // service worker/PWA documentation. 41 navigator.serviceWorker.ready.then(() => { 42 console.log( 43 "This web app is being served cache-first by a service " + 44 "worker. To learn more, visit https://bit.ly/CRA-PWA" 45 ); 46 }); 47 } else { 48 // Is not localhost. Just register service worker 49 registerValidSW(swUrl, config); 50 } 51 }); 52 } 53 } 54 55 function registerValidSW(swUrl, config) { 56 navigator.serviceWorker 57 .register(swUrl) 58 .then((registration) => { 59 registration.onupdatefound = () => { 60 const installingWorker = registration.installing; 61 if (installingWorker == null) { 62 return; 63 } 64 installingWorker.onstatechange = () => { 65 if (installingWorker.state === "installed") { 66 if (navigator.serviceWorker.controller) { 67 // At this point, the updated precached content has been fetched, 68 // but the previous service worker will still serve the older 69 // content until all client tabs are closed. 70 console.log( 71 "New content is available and will be used when all " + 72 "tabs for this page are closed. See https://bit.ly/CRA-PWA." 73 ); 74 75 // Execute callback 76 if (config && config.onUpdate) { 77 config.onUpdate(registration); 78 } 79 } else { 80 // At this point, everything has been precached. 81 // It's the perfect time to display a 82 // "Content is cached for offline use." message. 83 console.log("Content is cached for offline use."); 84 85 // Execute callback 86 if (config && config.onSuccess) { 87 config.onSuccess(registration); 88 } 89 } 90 } 91 }; 92 }; 93 }) 94 .catch((error) => { 95 console.error("Error during service worker registration:", error); 96 }); 97 } 98 99 function checkValidServiceWorker(swUrl, config) { 100 // Check if the service worker can be found. If it can't reload the page. 101 fetch(swUrl) 102 .then((response) => { 103 // Ensure service worker exists, and that we really are getting a JS file. 104 const contentType = response.headers.get("content-type"); 105 if ( 106 response.status === 404 || 107 (contentType != null && contentType.indexOf("javascript") === -1) 108 ) { 109 // No service worker found. Probably a different app. Reload the page. 110 navigator.serviceWorker.ready.then((registration) => { 111 registration.unregister().then(() => { 112 window.location.reload(); 113 }); 114 }); 115 } else { 116 // Service worker found. Proceed as normal. 117 registerValidSW(swUrl, config); 118 } 119 }) 120 .catch(() => { 121 console.log("No internet connection found. App is running in offline mode."); 122 }); 123 } 124 125 export function unregister() { 126 if ("serviceWorker" in navigator) { 127 navigator.serviceWorker.ready.then((registration) => { 128 registration.unregister(); 129 }); 130 } 131 }