(config)
| 40 | ) |
| 41 | |
| 42 | export function register (config) { |
| 43 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { |
| 44 | // The URL constructor is available in all browsers that support SW. |
| 45 | // @ts-ignore |
| 46 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href) |
| 47 | if (publicUrl.origin !== window.location.origin) { |
| 48 | // Our service worker won't work if PUBLIC_URL is on a different origin |
| 49 | // from what our page is served on. This might happen if a CDN is used to |
| 50 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | window.addEventListener('load', () => { |
| 55 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js` |
| 56 | |
| 57 | if (isLocalhost) { |
| 58 | // This is running on localhost. Let's check if a service worker still exists or not. |
| 59 | checkValidServiceWorker(swUrl, config) |
| 60 | |
| 61 | // Add some additional logging to localhost, pointing developers to the |
| 62 | // service worker/PWA documentation. |
| 63 | navigator.serviceWorker.ready.then(() => { |
| 64 | console.log( |
| 65 | 'This web app is being served cache-first by a service ' + |
| 66 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' |
| 67 | ) |
| 68 | }) |
| 69 | } else { |
| 70 | // Is not localhost. Just register service worker |
| 71 | registerValidSW(swUrl, config) |
| 72 | } |
| 73 | }) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | function registerValidSW (swUrl, config) { |
| 78 | navigator.serviceWorker |
nothing calls this directly
no test coverage detected