1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | const isLocalhost = Boolean(
|
14 | window.location.hostname === 'localhost' ||
|
15 |
|
16 | window.location.hostname === '[::1]' ||
|
17 |
|
18 | window.location.hostname.match(
|
19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
|
20 | )
|
21 | );
|
22 |
|
23 | type Config = {
|
24 | onSuccess?: (registration: ServiceWorkerRegistration) => void;
|
25 | onUpdate?: (registration: ServiceWorkerRegistration) => void;
|
26 | };
|
27 |
|
28 | export function register(config?: Config) {
|
29 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {
|
30 |
|
31 | const publicUrl = new URL(
|
32 | (process as { env: { [key: string]: string } }).env.PUBLIC_URL,
|
33 | window.location.href
|
34 | );
|
35 | if (publicUrl.origin !== window.location.origin) {
|
36 |
|
37 |
|
38 |
|
39 | return;
|
40 | }
|
41 |
|
42 | window.addEventListener('load', () => {
|
43 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;
|
44 |
|
45 | if (isLocalhost) {
|
46 |
|
47 | checkValidServiceWorker(swUrl, config);
|
48 |
|
49 |
|
50 |
|
51 | navigator.serviceWorker.ready.then(() => {
|
52 | console.log(
|
53 | 'This web app is being served cache-first by a service ' +
|
54 | 'worker. To learn more, visit http://bit.ly/CRA-PWA'
|
55 | );
|
56 | });
|
57 | } else {
|
58 |
|
59 | registerValidSW(swUrl, config);
|
60 | }
|
61 | });
|
62 | }
|
63 | }
|
64 |
|
65 | function registerValidSW(swUrl: string, config?: Config) {
|
66 | navigator.serviceWorker
|
67 | .register(swUrl)
|
68 | .then(registration => {
|
69 | registration.onupdatefound = () => {
|
70 | const installingWorker = registration.installing;
|
71 | if (installingWorker == null) {
|
72 | return;
|
73 | }
|
74 | installingWorker.onstatechange = () => {
|
75 | if (installingWorker.state === 'installed') {
|
76 | if (navigator.serviceWorker.controller) {
|
77 |
|
78 |
|
79 |
|
80 | console.log(
|
81 | 'New content is available and will be used when all ' +
|
82 | 'tabs for this page are closed. See http://bit.ly/CRA-PWA.'
|
83 | );
|
84 |
|
85 |
|
86 | if (config && config.onUpdate) {
|
87 | config.onUpdate(registration);
|
88 | }
|
89 | } else {
|
90 |
|
91 |
|
92 |
|
93 | console.log('Content is cached for offline use.');
|
94 |
|
95 |
|
96 | if (config && config.onSuccess) {
|
97 | config.onSuccess(registration);
|
98 | }
|
99 | }
|
100 | }
|
101 | };
|
102 | };
|
103 | })
|
104 | .catch(error => {
|
105 | console.error('Error during service worker registration:', error);
|
106 | });
|
107 | }
|
108 |
|
109 | function checkValidServiceWorker(swUrl: string, config?: Config) {
|
110 |
|
111 | fetch(swUrl)
|
112 | .then(response => {
|
113 |
|
114 | const contentType = response.headers.get('content-type');
|
115 | if (
|
116 | response.status === 404 ||
|
117 | (contentType != null && contentType.indexOf('javascript') === -1)
|
118 | ) {
|
119 |
|
120 | navigator.serviceWorker.ready.then(registration => {
|
121 | registration.unregister().then(() => {
|
122 | window.location.reload();
|
123 | });
|
124 | });
|
125 | } else {
|
126 |
|
127 | registerValidSW(swUrl, config);
|
128 | }
|
129 | })
|
130 | .catch(() => {
|
131 | console.log(
|
132 | 'No internet connection found. App is running in offline mode.'
|
133 | );
|
134 | });
|
135 | }
|
136 |
|
137 | export function unregister() {
|
138 | if ('serviceWorker' in navigator) {
|
139 | navigator.serviceWorker.ready.then(registration => {
|
140 | registration.unregister();
|
141 | });
|
142 | }
|
143 | }
|