|
11 | 11 | export default function register() {
|
12 | 12 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
|
13 | 13 | window.addEventListener('load', () => {
|
| 14 | + // this would become service-work-hash.js |
14 | 15 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
|
15 |
| - navigator.serviceWorker |
16 |
| - .register(swUrl) |
17 |
| - .then(registration => { |
18 |
| - registration.onupdatefound = () => { |
19 |
| - const installingWorker = registration.installing; |
20 |
| - installingWorker.onstatechange = () => { |
21 |
| - if (installingWorker.state === 'installed') { |
22 |
| - if (navigator.serviceWorker.controller) { |
23 |
| - // At this point, the old content will have been purged and |
24 |
| - // the fresh content will have been added to the cache. |
25 |
| - // It's the perfect time to display a "New content is |
26 |
| - // available; please refresh." message in your web app. |
27 |
| - console.log('New content is available; please refresh.'); |
28 |
| - } else { |
29 |
| - // At this point, everything has been precached. |
30 |
| - // It's the perfect time to display a |
31 |
| - // "Content is cached for offline use." message. |
32 |
| - console.log('Content is cached for offline use.'); |
33 |
| - } |
| 16 | + if (!navigator.serviceWorker.controller) { |
| 17 | + // No service worker yet |
| 18 | + registerServiceWorker(swUrl); |
| 19 | + } else { |
| 20 | + fetch(swUrl).then(res => { |
| 21 | + // Check to see if the SW URL is valid |
| 22 | + if (res.ok) { |
| 23 | + // Matches. All good. Continue with registering SW |
| 24 | + registerServiceWorker(swUrl); |
| 25 | + } else { |
| 26 | + // SW URL was invalid. |
| 27 | + fetch( |
| 28 | + `${window.location.protocol}//${window.location.host}` |
| 29 | + ).then(res2 => { |
| 30 | + // Just check if online |
| 31 | + if (res2.ok) { |
| 32 | + // Unregister and refresh page |
| 33 | + unregister(); |
| 34 | + window.location.reload(true); |
| 35 | + } else { |
| 36 | + console.log('Offline. Using cached copy'); |
34 | 37 | }
|
35 |
| - }; |
36 |
| - }; |
37 |
| - }) |
38 |
| - .catch(error => { |
39 |
| - console.error('Error during service worker registration:', error); |
| 38 | + }); |
| 39 | + } |
40 | 40 | });
|
| 41 | + } |
41 | 42 | });
|
42 | 43 | }
|
43 | 44 | }
|
44 | 45 |
|
| 46 | +function registerServiceWorker(url) { |
| 47 | + navigator.serviceWorker |
| 48 | + .register(url) |
| 49 | + .then(registration => { |
| 50 | + console.log('reg.scope', registration.scope); |
| 51 | + registration.onupdatefound = () => { |
| 52 | + const installingWorker = registration.installing; |
| 53 | + installingWorker.onstatechange = () => { |
| 54 | + if (installingWorker.state === 'installed') { |
| 55 | + if (navigator.serviceWorker.controller) { |
| 56 | + // At this point, the old content will have been purged and |
| 57 | + // the fresh content will have been added to the cache. |
| 58 | + // It's the perfect time to display a "New content is |
| 59 | + // available; please refresh." message in your web app. |
| 60 | + console.log('New content is available; please refresh.'); |
| 61 | + } else { |
| 62 | + // At this point, everything has been precached. |
| 63 | + // It's the perfect time to display a |
| 64 | + // "Content is cached for offline use." message. |
| 65 | + console.log('Content is cached for offline use.'); |
| 66 | + } |
| 67 | + } |
| 68 | + }; |
| 69 | + }; |
| 70 | + }) |
| 71 | + .catch(error => { |
| 72 | + console.log('No service worker found'); |
| 73 | + console.error('Error during service worker registration:', error); |
| 74 | + }); |
| 75 | +} |
| 76 | + |
45 | 77 | export function unregister() {
|
46 | 78 | if ('serviceWorker' in navigator) {
|
47 | 79 | navigator.serviceWorker.ready.then(registration => {
|
|
0 commit comments