88// resources are updated in the background.
99
1010// 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
1212
1313const isLocalhost = Boolean (
14- window . location . hostname === " localhost" ||
14+ window . location . hostname === ' localhost' ||
1515 // [::1] is the IPv6 localhost address.
16- window . location . hostname === " [::1]" ||
16+ window . location . hostname === ' [::1]' ||
1717 // 127.0.0.1/8 is considered localhost for IPv4.
1818 window . location . hostname . match (
1919 / ^ 1 2 7 (?: \. (?: 2 5 [ 0 - 5 ] | 2 [ 0 - 4 ] [ 0 - 9 ] | [ 0 1 ] ? [ 0 - 9 ] [ 0 - 9 ] ? ) ) { 3 } $ /
2020 )
2121) ;
2222
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 ) {
2530 // 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+ ) ;
2735 if ( publicUrl . origin !== window . location . origin ) {
2836 // Our service worker won't work if PUBLIC_URL is on a different origin
2937 // from what our page is served on. This might happen if a CDN is used to
3038 // serve assets; see https://github.com/facebook/create-react-app/issues/2374
3139 return ;
3240 }
3341
34- window . addEventListener ( " load" , ( ) => {
42+ window . addEventListener ( ' load' , ( ) => {
3543 const swUrl = `${ process . env . PUBLIC_URL } /service-worker.js` ;
3644
3745 if ( isLocalhost ) {
@@ -42,8 +50,8 @@ export function register(config) {
4250 // service worker/PWA documentation.
4351 navigator . serviceWorker . ready . then ( ( ) => {
4452 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'
4755 ) ;
4856 } ) ;
4957 } else {
@@ -54,7 +62,7 @@ export function register(config) {
5462 }
5563}
5664
57- function registerValidSW ( swUrl , config ) {
65+ function registerValidSW ( swUrl : string , config ?: Config ) {
5866 navigator . serviceWorker
5967 . register ( swUrl )
6068 . then ( registration => {
@@ -64,14 +72,14 @@ function registerValidSW(swUrl, config) {
6472 return ;
6573 }
6674 installingWorker . onstatechange = ( ) => {
67- if ( installingWorker . state === " installed" ) {
75+ if ( installingWorker . state === ' installed' ) {
6876 if ( navigator . serviceWorker . controller ) {
6977 // At this point, the updated precached content has been fetched,
7078 // but the previous service worker will still serve the older
7179 // content until all client tabs are closed.
7280 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.'
7583 ) ;
7684
7785 // Execute callback
@@ -82,7 +90,7 @@ function registerValidSW(swUrl, config) {
8290 // At this point, everything has been precached.
8391 // It's the perfect time to display a
8492 // "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.' ) ;
8694
8795 // Execute callback
8896 if ( config && config . onSuccess ) {
@@ -94,19 +102,19 @@ function registerValidSW(swUrl, config) {
94102 } ;
95103 } )
96104 . catch ( error => {
97- console . error ( " Error during service worker registration:" , error ) ;
105+ console . error ( ' Error during service worker registration:' , error ) ;
98106 } ) ;
99107}
100108
101- function checkValidServiceWorker ( swUrl , config ) {
109+ function checkValidServiceWorker ( swUrl : string , config ?: Config ) {
102110 // Check if the service worker can be found. If it can't reload the page.
103111 fetch ( swUrl )
104112 . then ( response => {
105113 // 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' ) ;
107115 if (
108116 response . status === 404 ||
109- ( contentType != null && contentType . indexOf ( " javascript" ) === - 1 )
117+ ( contentType != null && contentType . indexOf ( ' javascript' ) === - 1 )
110118 ) {
111119 // No service worker found. Probably a different app. Reload the page.
112120 navigator . serviceWorker . ready . then ( registration => {
@@ -121,13 +129,13 @@ function checkValidServiceWorker(swUrl, config) {
121129 } )
122130 . catch ( ( ) => {
123131 console . log (
124- " No internet connection found. App is running in offline mode."
132+ ' No internet connection found. App is running in offline mode.'
125133 ) ;
126134 } ) ;
127135}
128136
129137export function unregister ( ) {
130- if ( " serviceWorker" in navigator ) {
138+ if ( ' serviceWorker' in navigator ) {
131139 navigator . serviceWorker . ready . then ( registration => {
132140 registration . unregister ( ) ;
133141 } ) ;
0 commit comments