(swUrl, config)
| 119 | } |
| 120 | |
| 121 | function checkValidServiceWorker (swUrl, config) { |
| 122 | // Check if the service worker can be found. If it can't reload the page. |
| 123 | window.fetch(swUrl, { |
| 124 | headers: { 'Service-Worker': 'script' } |
| 125 | }) |
| 126 | .then(response => { |
| 127 | // Ensure service worker exists, and that we really are getting a JS file. |
| 128 | const contentType = response.headers.get('content-type') |
| 129 | if ( |
| 130 | response.status === 404 || |
| 131 | (contentType != null && !contentType.includes('javascript')) |
| 132 | ) { |
| 133 | // No service worker found. Probably a different app. Reload the page. |
| 134 | navigator.serviceWorker.ready.then(registration => { |
| 135 | registration.unregister().then(() => { |
| 136 | window.location.reload() |
| 137 | }) |
| 138 | }) |
| 139 | } else { |
| 140 | // Service worker found. Proceed as normal. |
| 141 | registerValidSW(swUrl, config) |
| 142 | } |
| 143 | }) |
| 144 | .catch(() => { |
| 145 | console.log( |
| 146 | 'No internet connection found. App is running in offline mode.' |
| 147 | ) |
| 148 | }) |
| 149 | } |
| 150 | |
| 151 | export function unregister () { |
| 152 | if ('serviceWorker' in navigator) { |
no test coverage detected