(swUrl, config)
| 96 | } |
| 97 | |
| 98 | function checkValidServiceWorker(swUrl, config) { |
| 99 | // Check if the service worker can be found. If it can't reload the page. |
| 100 | fetch(swUrl) |
| 101 | .then((response) => { |
| 102 | // Ensure service worker exists, and that we really are getting a JS file. |
| 103 | if ( |
| 104 | response.status === 404 |
| 105 | || response.headers.get('content-type').indexOf('javascript') === -1 |
| 106 | ) { |
| 107 | // No service worker found. Probably a different app. Reload the page. |
| 108 | navigator.serviceWorker.ready.then((registration) => { |
| 109 | registration.unregister().then(() => { |
| 110 | window.location.reload(); |
| 111 | }); |
| 112 | }); |
| 113 | } else { |
| 114 | // Service worker found. Proceed as normal. |
| 115 | registerValidSW(swUrl, config); |
| 116 | } |
| 117 | }) |
| 118 | .catch(() => { |
| 119 | console.log( |
| 120 | 'No internet connection found. App is running in offline mode.' |
| 121 | ); |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | export function unregister() { |
| 126 | if ('serviceWorker' in navigator) { |
no test coverage detected