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