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