| 76 | } |
| 77 | |
| 78 | function registerValidSW(swUrl: string, config?: Config) { |
| 79 | navigator.serviceWorker |
| 80 | .register(swUrl) |
| 81 | .then((registration) => { |
| 82 | registration.onupdatefound = () => { |
| 83 | const installingWorker = registration.installing; |
| 84 | if (installingWorker == null) { |
| 85 | return; |
| 86 | } |
| 87 | installingWorker.onstatechange = () => { |
| 88 | if (installingWorker.state === 'installed') { |
| 89 | if (navigator.serviceWorker.controller) { |
| 90 | // At this point, the updated precached content has been fetched, |
| 91 | // but the previous service worker will still serve the older |
| 92 | // content until all client tabs are closed. |
| 93 | console.log('New content is available and will be used when all ' + 'tabs for this page are closed. See https://bit.ly/CRA-PWA.'); |
| 94 | |
| 95 | // Execute callback |
| 96 | if (config && config.onUpdate) { |
| 97 | config.onUpdate(registration); |
| 98 | } |
| 99 | } else { |
| 100 | // At this point, everything has been precached. |
| 101 | // It's the perfect time to display a |
| 102 | // "Content is cached for offline use." message. |
| 103 | console.log('Content is cached for offline use.'); |
| 104 | |
| 105 | // Execute callback |
| 106 | if (config && config.onSuccess) { |
| 107 | config.onSuccess(registration); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | }; |
| 112 | }; |
| 113 | }) |
| 114 | .catch((error) => { |
| 115 | console.warn('! ' + 'Error during service worker registration:', error); |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | function checkValidServiceWorker(swUrl: string, config?: Config) { |
| 120 | // Check if the service worker can be found. If it can't reload the page. |