|
10 | 10 | // This link also includes instructions on opting out of this behavior. |
11 | 11 |
|
12 | 12 | const isLocalhost = Boolean( |
13 | | - window.location.hostname === 'localhost' || |
| 13 | + window.location.hostname === 'localhost' || |
14 | 14 | // [::1] is the IPv6 localhost address. |
15 | 15 | window.location.hostname === '[::1]' || |
16 | 16 | // 127.0.0.1/8 is considered localhost for IPv4. |
17 | 17 | window.location.hostname.match( |
18 | | - /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ |
| 18 | + /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ |
19 | 19 | ) |
20 | 20 | ); |
21 | 21 |
|
22 | 22 | export default function register() { |
23 | | - if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { |
24 | | - // The URL constructor is available in all browsers that support SW. |
25 | | - const publicUrl = new URL( |
26 | | - process.env.PUBLIC_URL!, |
27 | | - window.location.toString() |
28 | | - ); |
29 | | - if (publicUrl.origin !== window.location.origin) { |
30 | | - // Our service worker won't work if PUBLIC_URL is on a different origin |
31 | | - // from what our page is served on. This might happen if a CDN is used to |
32 | | - // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374 |
33 | | - return; |
34 | | - } |
| 23 | + if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { |
| 24 | + // The URL constructor is available in all browsers that support SW. |
| 25 | + const publicUrl = new URL( |
| 26 | + process.env.PUBLIC_URL!, |
| 27 | + window.location.toString() |
| 28 | + ); |
| 29 | + if (publicUrl.origin !== window.location.origin) { |
| 30 | + // Our service worker won't work if PUBLIC_URL is on a different origin |
| 31 | + // from what our page is served on. This might happen if a CDN is used to |
| 32 | + // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374 |
| 33 | + return; |
| 34 | + } |
35 | 35 |
|
36 | | - window.addEventListener('load', () => { |
37 | | - const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; |
| 36 | + window.addEventListener('load', () => { |
| 37 | + const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; |
38 | 38 |
|
39 | | - if (!isLocalhost) { |
40 | | - // Is not local host. Just register service worker |
41 | | - registerValidSW(swUrl); |
42 | | - } else { |
43 | | - // This is running on localhost. Lets check if a service worker still exists or not. |
44 | | - checkValidServiceWorker(swUrl); |
45 | | - } |
46 | | - }); |
47 | | - } |
| 39 | + if (!isLocalhost) { |
| 40 | + // Is not local host. Just register service worker |
| 41 | + registerValidSW(swUrl); |
| 42 | + } else { |
| 43 | + // This is running on localhost. Lets check if a service worker still exists or not. |
| 44 | + checkValidServiceWorker(swUrl); |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
48 | 48 | } |
49 | 49 |
|
50 | 50 | function registerValidSW(swUrl: string) { |
51 | | - navigator.serviceWorker |
52 | | - .register(swUrl) |
53 | | - .then((registration) => { |
54 | | - registration.onupdatefound = () => { |
55 | | - const installingWorker = registration.installing; |
56 | | - if (installingWorker) { |
57 | | - installingWorker.onstatechange = () => { |
58 | | - if (installingWorker.state === 'installed') { |
59 | | - if (navigator.serviceWorker.controller) { |
60 | | - // At this point, the old content will have been purged and |
61 | | - // the fresh content will have been added to the cache. |
62 | | - // It's the perfect time to display a 'New content is |
63 | | - // available; please refresh.' message in your web app. |
64 | | - console.log('New content is available; please refresh.'); |
65 | | - } else { |
66 | | - // At this point, everything has been precached. |
67 | | - // It's the perfect time to display a |
68 | | - // 'Content is cached for offline use.' message. |
69 | | - console.log('Content is cached for offline use.'); |
70 | | - } |
71 | | - } |
72 | | - }; |
73 | | - } |
74 | | - }; |
75 | | - }) |
76 | | - .catch((error) => { |
77 | | - console.error('Error during service worker registration:', error); |
78 | | - }); |
| 51 | + navigator.serviceWorker |
| 52 | + .register(swUrl) |
| 53 | + .then(registration => { |
| 54 | + registration.onupdatefound = () => { |
| 55 | + const installingWorker = registration.installing; |
| 56 | + if (installingWorker) { |
| 57 | + installingWorker.onstatechange = () => { |
| 58 | + if (installingWorker.state === 'installed') { |
| 59 | + if (navigator.serviceWorker.controller) { |
| 60 | + // At this point, the old content will have been purged and |
| 61 | + // the fresh content will have been added to the cache. |
| 62 | + // It's the perfect time to display a 'New content is |
| 63 | + // available; please refresh.' message in your web app. |
| 64 | + console.log('New content is available; please refresh.'); |
| 65 | + } else { |
| 66 | + // At this point, everything has been precached. |
| 67 | + // It's the perfect time to display a |
| 68 | + // 'Content is cached for offline use.' message. |
| 69 | + console.log('Content is cached for offline use.'); |
| 70 | + } |
| 71 | + } |
| 72 | + }; |
| 73 | + } |
| 74 | + }; |
| 75 | + }) |
| 76 | + .catch(error => { |
| 77 | + console.error('Error during service worker registration:', error); |
| 78 | + }); |
79 | 79 | } |
80 | 80 |
|
81 | 81 | function checkValidServiceWorker(swUrl: string) { |
82 | | - // Check if the service worker can be found. If it can't reload the page. |
83 | | - fetch(swUrl) |
84 | | - .then((response) => { |
85 | | - // Ensure service worker exists, and that we really are getting a JS file. |
86 | | - if ( |
87 | | - response.status === 404 || |
88 | | - response.headers.get('content-type')!.indexOf('javascript') === -1 |
89 | | - ) { |
90 | | - // No service worker found. Probably a different app. Reload the page. |
91 | | - navigator.serviceWorker.ready.then((registration) => { |
92 | | - registration.unregister().then(() => { |
93 | | - window.location.reload(); |
94 | | - }); |
95 | | - }); |
96 | | - } else { |
97 | | - // Service worker found. Proceed as normal. |
98 | | - registerValidSW(swUrl); |
99 | | - } |
100 | | - }) |
101 | | - .catch(() => { |
102 | | - console.log( |
103 | | - 'No internet connection found. App is running in offline mode.' |
104 | | - ); |
| 82 | + // Check if the service worker can be found. If it can't reload the page. |
| 83 | + fetch(swUrl) |
| 84 | + .then(response => { |
| 85 | + // Ensure service worker exists, and that we really are getting a JS file. |
| 86 | + if ( |
| 87 | + response.status === 404 || |
| 88 | + response.headers.get('content-type')!.indexOf('javascript') === -1 |
| 89 | + ) { |
| 90 | + // No service worker found. Probably a different app. Reload the page. |
| 91 | + navigator.serviceWorker.ready.then(registration => { |
| 92 | + registration.unregister().then(() => { |
| 93 | + window.location.reload(); |
| 94 | + }); |
105 | 95 | }); |
| 96 | + } else { |
| 97 | + // Service worker found. Proceed as normal. |
| 98 | + registerValidSW(swUrl); |
| 99 | + } |
| 100 | + }) |
| 101 | + .catch(() => { |
| 102 | + console.log( |
| 103 | + 'No internet connection found. App is running in offline mode.' |
| 104 | + ); |
| 105 | + }); |
106 | 106 | } |
107 | 107 |
|
108 | 108 | export function unregister() { |
109 | | - if ('serviceWorker' in navigator) { |
110 | | - navigator.serviceWorker.ready.then((registration) => { |
111 | | - registration.unregister(); |
112 | | - }); |
113 | | - } |
| 109 | + if ('serviceWorker' in navigator) { |
| 110 | + navigator.serviceWorker.ready.then(registration => { |
| 111 | + registration.unregister(); |
| 112 | + }); |
| 113 | + } |
114 | 114 | } |
0 commit comments