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