|
1 | | -// /* eslint-disable */ |
2 | | -// // This optional code is used to register a service worker. |
3 | | -// // register() is not called by default. |
| 1 | +/* eslint-disable */ |
| 2 | +// This optional code is used to register a service worker. |
| 3 | +// register() is not called by default. |
4 | 4 |
|
5 | | -// // This lets the app load faster on subsequent visits in production, and gives |
6 | | -// // it offline capabilities. However, it also means that developers (and users) |
7 | | -// // will only see deployed updates on subsequent visits to a page, after all the |
8 | | -// // existing tabs open on the page have been closed, since previously cached |
9 | | -// // resources are updated in the background. |
| 5 | +// This lets the app load faster on subsequent visits in production, and gives |
| 6 | +// it offline capabilities. However, it also means that developers (and users) |
| 7 | +// will only see deployed updates on subsequent visits to a page, after all the |
| 8 | +// existing tabs open on the page have been closed, since previously cached |
| 9 | +// resources are updated in the background. |
10 | 10 |
|
11 | | -// // To learn more about the benefits of this model and instructions on how to |
12 | | -// // opt-in, read https://cra.link/PWA |
| 11 | +// To learn more about the benefits of this model and instructions on how to |
| 12 | +// opt-in, read https://cra.link/PWA |
13 | 13 |
|
14 | | -// const isLocalhost = Boolean( |
15 | | -// window.location.hostname === 'localhost' || |
16 | | -// // [::1] is the IPv6 localhost address. |
17 | | -// window.location.hostname === '[::1]' || |
18 | | -// // 127.0.0.0/8 are considered localhost for IPv4. |
19 | | -// window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/) |
20 | | -// ); |
| 14 | +const isLocalhost = Boolean( |
| 15 | + window.location.hostname === 'localhost' || |
| 16 | + // [::1] is the IPv6 localhost address. |
| 17 | + window.location.hostname === '[::1]' || |
| 18 | + // 127.0.0.0/8 are considered localhost for IPv4. |
| 19 | + window.location.hostname.match(/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/) |
| 20 | +); |
21 | 21 |
|
22 | | -// export function register(config) { |
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(process.env.PUBLIC_URL, window.location.href); |
26 | | -// if (publicUrl.origin !== window.location.origin) { |
27 | | -// // Our service worker won't work if PUBLIC_URL is on a different origin |
28 | | -// // from what our page is served on. This might happen if a CDN is used to |
29 | | -// // serve assets; see https://github.com/facebook/create-react-app/issues/2374 |
30 | | -// return; |
31 | | -// } |
| 22 | +export function register(config) { |
| 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(process.env.PUBLIC_URL, window.location.href); |
| 26 | + if (publicUrl.origin !== window.location.origin) { |
| 27 | + // Our service worker won't work if PUBLIC_URL is on a different origin |
| 28 | + // from what our page is served on. This might happen if a CDN is used to |
| 29 | + // serve assets; see https://github.com/facebook/create-react-app/issues/2374 |
| 30 | + return; |
| 31 | + } |
32 | 32 |
|
33 | | -// window.addEventListener('load', () => { |
34 | | -// const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; |
| 33 | + window.addEventListener('load', () => { |
| 34 | + const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; |
35 | 35 |
|
36 | | -// if (isLocalhost) { |
37 | | -// // This is running on localhost. Let's check if a service worker still exists or not. |
38 | | -// checkValidServiceWorker(swUrl, config); |
| 36 | + if (isLocalhost) { |
| 37 | + // This is running on localhost. Let's check if a service worker still exists or not. |
| 38 | + checkValidServiceWorker(swUrl, config); |
39 | 39 |
|
40 | | -// // Add some additional logging to localhost, pointing developers to the |
41 | | -// // service worker/PWA documentation. |
42 | | -// navigator.serviceWorker.ready.then(() => { |
43 | | -// console.log( |
44 | | -// 'This web app is being served cache-first by a service ' + |
45 | | -// 'worker. To learn more, visit https://cra.link/PWA' |
46 | | -// ); |
47 | | -// }); |
48 | | -// } else { |
49 | | -// // Is not localhost. Just register service worker |
50 | | -// registerValidSW(swUrl, config); |
51 | | -// } |
52 | | -// }); |
53 | | -// } |
54 | | -// } |
| 40 | + // Add some additional logging to localhost, pointing developers to the |
| 41 | + // service worker/PWA documentation. |
| 42 | + navigator.serviceWorker.ready.then(() => { |
| 43 | + console.log( |
| 44 | + 'This web app is being served cache-first by a service ' + |
| 45 | + 'worker. To learn more, visit https://cra.link/PWA' |
| 46 | + ); |
| 47 | + }); |
| 48 | + } else { |
| 49 | + // Is not localhost. Just register service worker |
| 50 | + registerValidSW(swUrl, config); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | +} |
55 | 55 |
|
56 | | -// function registerValidSW(swUrl, config) { |
57 | | -// navigator.serviceWorker |
58 | | -// .register(swUrl) |
59 | | -// .then((registration) => { |
60 | | -// registration.onupdatefound = () => { |
61 | | -// const installingWorker = registration.installing; |
62 | | -// if (installingWorker == null) { |
63 | | -// return; |
64 | | -// } |
65 | | -// installingWorker.onstatechange = () => { |
66 | | -// if (installingWorker.state === 'installed') { |
67 | | -// if (navigator.serviceWorker.controller) { |
68 | | -// // At this point, the updated precached content has been fetched, |
69 | | -// // but the previous service worker will still serve the older |
70 | | -// // content until all client tabs are closed. |
71 | | -// console.log( |
72 | | -// 'New content is available and will be used when all ' + |
73 | | -// 'tabs for this page are closed. See https://cra.link/PWA.' |
74 | | -// ); |
| 56 | +function registerValidSW(swUrl, config) { |
| 57 | + navigator.serviceWorker |
| 58 | + .register(swUrl) |
| 59 | + .then((registration) => { |
| 60 | + registration.onupdatefound = () => { |
| 61 | + const installingWorker = registration.installing; |
| 62 | + if (installingWorker == null) { |
| 63 | + return; |
| 64 | + } |
| 65 | + installingWorker.onstatechange = () => { |
| 66 | + if (installingWorker.state === 'installed') { |
| 67 | + if (navigator.serviceWorker.controller) { |
| 68 | + // At this point, the updated precached content has been fetched, |
| 69 | + // but the previous service worker will still serve the older |
| 70 | + // content until all client tabs are closed. |
| 71 | + console.log( |
| 72 | + 'New content is available and will be used when all ' + |
| 73 | + 'tabs for this page are closed. See https://cra.link/PWA.' |
| 74 | + ); |
75 | 75 |
|
76 | | -// // Execute callback |
77 | | -// if (config && config.onUpdate) { |
78 | | -// config.onUpdate(registration); |
79 | | -// } |
80 | | -// } else { |
81 | | -// // At this point, everything has been precached. |
82 | | -// // It's the perfect time to display a |
83 | | -// // "Content is cached for offline use." message. |
84 | | -// console.log('Content is cached for offline use.'); |
| 76 | + // Execute callback |
| 77 | + if (config && config.onUpdate) { |
| 78 | + config.onUpdate(registration); |
| 79 | + } |
| 80 | + } else { |
| 81 | + // At this point, everything has been precached. |
| 82 | + // It's the perfect time to display a |
| 83 | + // "Content is cached for offline use." message. |
| 84 | + console.log('Content is cached for offline use.'); |
85 | 85 |
|
86 | | -// // Execute callback |
87 | | -// if (config && config.onSuccess) { |
88 | | -// config.onSuccess(registration); |
89 | | -// } |
90 | | -// } |
91 | | -// } |
92 | | -// }; |
93 | | -// }; |
94 | | -// }) |
95 | | -// .catch((error) => { |
96 | | -// console.error('Error during service worker registration:', error); |
97 | | -// }); |
98 | | -// } |
| 86 | + // Execute callback |
| 87 | + if (config && config.onSuccess) { |
| 88 | + config.onSuccess(registration); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + }; |
| 93 | + }; |
| 94 | + }) |
| 95 | + .catch((error) => { |
| 96 | + console.error('Error during service worker registration:', error); |
| 97 | + }); |
| 98 | +} |
99 | 99 |
|
100 | | -// function checkValidServiceWorker(swUrl, config) { |
101 | | -// // Check if the service worker can be found. If it can't reload the page. |
102 | | -// fetch(swUrl, { |
103 | | -// headers: { 'Service-Worker': 'script' }, |
104 | | -// }) |
105 | | -// .then((response) => { |
106 | | -// // Ensure service worker exists, and that we really are getting a JS file. |
107 | | -// const contentType = response.headers.get('content-type'); |
108 | | -// if ( |
109 | | -// response.status === 404 || |
110 | | -// (contentType != null && contentType.indexOf('javascript') === -1) |
111 | | -// ) { |
112 | | -// // No service worker found. Probably a different app. Reload the page. |
113 | | -// navigator.serviceWorker.ready.then((registration) => { |
114 | | -// registration.unregister().then(() => { |
115 | | -// window.location.reload(); |
116 | | -// }); |
117 | | -// }); |
118 | | -// } else { |
119 | | -// // Service worker found. Proceed as normal. |
120 | | -// registerValidSW(swUrl, config); |
121 | | -// } |
122 | | -// }) |
123 | | -// .catch(() => { |
124 | | -// console.log('No internet connection found. App is running in offline mode.'); |
125 | | -// }); |
126 | | -// } |
| 100 | +function checkValidServiceWorker(swUrl, config) { |
| 101 | + // Check if the service worker can be found. If it can't reload the page. |
| 102 | + fetch(swUrl, { |
| 103 | + headers: { 'Service-Worker': 'script' }, |
| 104 | + }) |
| 105 | + .then((response) => { |
| 106 | + // Ensure service worker exists, and that we really are getting a JS file. |
| 107 | + const contentType = response.headers.get('content-type'); |
| 108 | + if ( |
| 109 | + response.status === 404 || |
| 110 | + (contentType != null && contentType.indexOf('javascript') === -1) |
| 111 | + ) { |
| 112 | + // No service worker found. Probably a different app. Reload the page. |
| 113 | + navigator.serviceWorker.ready.then((registration) => { |
| 114 | + registration.unregister().then(() => { |
| 115 | + window.location.reload(); |
| 116 | + }); |
| 117 | + }); |
| 118 | + } else { |
| 119 | + // Service worker found. Proceed as normal. |
| 120 | + registerValidSW(swUrl, config); |
| 121 | + } |
| 122 | + }) |
| 123 | + .catch(() => { |
| 124 | + console.log('No internet connection found. App is running in offline mode.'); |
| 125 | + }); |
| 126 | +} |
127 | 127 |
|
128 | | -// export function unregister() { |
129 | | -// if ('serviceWorker' in navigator) { |
130 | | -// navigator.serviceWorker.ready |
131 | | -// .then((registration) => { |
132 | | -// registration.unregister(); |
133 | | -// }) |
134 | | -// .catch((error) => { |
135 | | -// console.error(error.message); |
136 | | -// }); |
137 | | -// } |
138 | | -// } |
| 128 | +export function unregister() { |
| 129 | + if ('serviceWorker' in navigator) { |
| 130 | + navigator.serviceWorker.ready |
| 131 | + .then((registration) => { |
| 132 | + registration.unregister(); |
| 133 | + }) |
| 134 | + .catch((error) => { |
| 135 | + console.error(error.message); |
| 136 | + }); |
| 137 | + } |
| 138 | +} |
0 commit comments