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