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.
17
+ // 127.0.0.0 /8 are considered localhost for IPv4.
18
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
- )
19
+ / ^ 1 2 7 (?: \. (?: 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) ) { 3 } $ / ,
20
+ ) ,
21
21
) ;
22
22
23
- export function register ( config ) {
23
+ type Config = {
24
+ onSuccess ?: ( registration : ServiceWorkerRegistration ) => void ;
25
+ onUpdate ?: ( registration : ServiceWorkerRegistration ) => void ;
26
+ } ;
27
+
28
+ export function register ( config ?: Config ) : void {
24
29
if ( process . env . NODE_ENV === 'production' && 'serviceWorker' in navigator ) {
25
30
// The URL constructor is available in all browsers that support SW.
26
31
const publicUrl = new URL ( process . env . PUBLIC_URL , window . location . href ) ;
@@ -43,7 +48,7 @@ export function register(config) {
43
48
navigator . serviceWorker . ready . then ( ( ) => {
44
49
console . log (
45
50
'This web app is being served cache-first by a service ' +
46
- 'worker. To learn more, visit https://bit.ly/CRA- PWA'
51
+ 'worker. To learn more, visit https://cra.link/ PWA' ,
47
52
) ;
48
53
} ) ;
49
54
} else {
@@ -54,10 +59,10 @@ export function register(config) {
54
59
}
55
60
}
56
61
57
- function registerValidSW ( swUrl , config ) {
62
+ function registerValidSW ( swUrl : string , config ?: Config ) {
58
63
navigator . serviceWorker
59
64
. register ( swUrl )
60
- . then ( registration => {
65
+ . then ( ( registration ) => {
61
66
registration . onupdatefound = ( ) => {
62
67
const installingWorker = registration . installing ;
63
68
if ( installingWorker == null ) {
@@ -71,7 +76,7 @@ function registerValidSW(swUrl, config) {
71
76
// content until all client tabs are closed.
72
77
console . log (
73
78
'New content is available and will be used when all ' +
74
- 'tabs for this page are closed. See https://bit.ly/CRA- PWA.'
79
+ 'tabs for this page are closed. See https://cra.link/ PWA.' ,
75
80
) ;
76
81
77
82
// Execute callback
@@ -93,23 +98,25 @@ function registerValidSW(swUrl, config) {
93
98
} ;
94
99
} ;
95
100
} )
96
- . catch ( error => {
101
+ . catch ( ( error ) => {
97
102
console . error ( 'Error during service worker registration:' , error ) ;
98
103
} ) ;
99
104
}
100
105
101
- function checkValidServiceWorker ( swUrl , config ) {
106
+ function checkValidServiceWorker ( swUrl : string , config ?: Config ) {
102
107
// Check if the service worker can be found. If it can't reload the page.
103
- fetch ( swUrl )
104
- . then ( response => {
108
+ fetch ( swUrl , {
109
+ headers : { 'Service-Worker' : 'script' } ,
110
+ } )
111
+ . then ( ( response ) => {
105
112
// Ensure service worker exists, and that we really are getting a JS file.
106
113
const contentType = response . headers . get ( 'content-type' ) ;
107
114
if (
108
115
response . status === 404 ||
109
116
( contentType != null && contentType . indexOf ( 'javascript' ) === - 1 )
110
117
) {
111
118
// No service worker found. Probably a different app. Reload the page.
112
- navigator . serviceWorker . ready . then ( registration => {
119
+ navigator . serviceWorker . ready . then ( ( registration ) => {
113
120
registration . unregister ( ) . then ( ( ) => {
114
121
window . location . reload ( ) ;
115
122
} ) ;
@@ -120,16 +127,18 @@ function checkValidServiceWorker(swUrl, config) {
120
127
}
121
128
} )
122
129
. catch ( ( ) => {
123
- console . log (
124
- 'No internet connection found. App is running in offline mode.'
125
- ) ;
130
+ console . log ( 'No internet connection found. App is running in offline mode.' ) ;
126
131
} ) ;
127
132
}
128
133
129
- export function unregister ( ) {
134
+ export function unregister ( ) : void {
130
135
if ( 'serviceWorker' in navigator ) {
131
- navigator . serviceWorker . ready . then ( registration => {
132
- registration . unregister ( ) ;
133
- } ) ;
136
+ navigator . serviceWorker . ready
137
+ . then ( ( registration ) => {
138
+ registration . unregister ( ) ;
139
+ } )
140
+ . catch ( ( error ) => {
141
+ console . error ( error . message ) ;
142
+ } ) ;
134
143
}
135
144
}
0 commit comments