8
8
// resources are updated in the background.
9
9
10
10
// To learn more about the benefits of this model and instructions on how to
11
- // opt-in, read https://bit.ly/CRA- PWA
11
+ // opt-in, read https://cra.link/ PWA
12
12
13
13
const isLocalhost = Boolean (
14
14
window . location . hostname === 'localhost' ||
15
15
// [::1] is the IPv6 localhost address.
16
16
window . location . hostname === '[::1]' ||
17
- // 127.0.0.1/8 is considered localhost for IPv4.
18
- window . location . hostname . match (
19
- / ^ 1 2 7 (?: \. (?: 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) ) { 3 } $ /
20
- )
17
+ // 127.0.0.0/8 are considered localhost for IPv4.
18
+ window . location . hostname . match ( / ^ 1 2 7 (?: \. (?: 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) ) { 3 } $ / )
21
19
) ;
22
20
23
21
export function register ( config ) {
@@ -43,7 +41,7 @@ export function register(config) {
43
41
navigator . serviceWorker . ready . then ( ( ) => {
44
42
console . log (
45
43
'This web app is being served cache-first by a service ' +
46
- 'worker. To learn more, visit https://bit.ly/CRA- PWA'
44
+ 'worker. To learn more, visit https://cra.link/ PWA'
47
45
) ;
48
46
} ) ;
49
47
} else {
@@ -57,7 +55,7 @@ export function register(config) {
57
55
function registerValidSW ( swUrl , config ) {
58
56
navigator . serviceWorker
59
57
. register ( swUrl )
60
- . then ( registration => {
58
+ . then ( ( registration ) => {
61
59
registration . onupdatefound = ( ) => {
62
60
const installingWorker = registration . installing ;
63
61
if ( installingWorker == null ) {
@@ -71,7 +69,7 @@ function registerValidSW(swUrl, config) {
71
69
// content until all client tabs are closed.
72
70
console . log (
73
71
'New content is available and will be used when all ' +
74
- 'tabs for this page are closed. See https://bit.ly/CRA- PWA.'
72
+ 'tabs for this page are closed. See https://cra.link/ PWA.'
75
73
) ;
76
74
77
75
// Execute callback
@@ -93,23 +91,25 @@ function registerValidSW(swUrl, config) {
93
91
} ;
94
92
} ;
95
93
} )
96
- . catch ( error => {
94
+ . catch ( ( error ) => {
97
95
console . error ( 'Error during service worker registration:' , error ) ;
98
96
} ) ;
99
97
}
100
98
101
99
function checkValidServiceWorker ( swUrl , config ) {
102
100
// Check if the service worker can be found. If it can't reload the page.
103
- fetch ( swUrl )
104
- . then ( response => {
101
+ fetch ( swUrl , {
102
+ headers : { 'Service-Worker' : 'script' } ,
103
+ } )
104
+ . then ( ( response ) => {
105
105
// Ensure service worker exists, and that we really are getting a JS file.
106
106
const contentType = response . headers . get ( 'content-type' ) ;
107
107
if (
108
108
response . status === 404 ||
109
109
( contentType != null && contentType . indexOf ( 'javascript' ) === - 1 )
110
110
) {
111
111
// No service worker found. Probably a different app. Reload the page.
112
- navigator . serviceWorker . ready . then ( registration => {
112
+ navigator . serviceWorker . ready . then ( ( registration ) => {
113
113
registration . unregister ( ) . then ( ( ) => {
114
114
window . location . reload ( ) ;
115
115
} ) ;
@@ -120,16 +120,18 @@ function checkValidServiceWorker(swUrl, config) {
120
120
}
121
121
} )
122
122
. catch ( ( ) => {
123
- console . log (
124
- 'No internet connection found. App is running in offline mode.'
125
- ) ;
123
+ console . log ( 'No internet connection found. App is running in offline mode.' ) ;
126
124
} ) ;
127
125
}
128
126
129
127
export function unregister ( ) {
130
128
if ( 'serviceWorker' in navigator ) {
131
- navigator . serviceWorker . ready . then ( registration => {
132
- registration . unregister ( ) ;
133
- } ) ;
129
+ navigator . serviceWorker . ready
130
+ . then ( ( registration ) => {
131
+ registration . unregister ( ) ;
132
+ } )
133
+ . catch ( ( error ) => {
134
+ console . error ( error . message ) ;
135
+ } ) ;
134
136
}
135
137
}
0 commit comments