1010// To learn more about the benefits of this model and instructions on how to
1111// opt-in, read https://bit.ly/CRA-PWA
1212
13- const isLocalhost = Boolean (
14- window . location . hostname === "localhost" ||
15- // [::1] is the IPv6 localhost address.
16- window . location . hostname === "[::1]" ||
17- // 127.0.0.0/8 are 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- )
21- ) ;
13+ const isLocalhost =
14+ Boolean ( window . location . hostname === "localhost" ||
15+ // [::1] is the IPv6 localhost address.
16+ window . location . hostname === "[::1]" ||
17+ // 127.0.0.0/8 are 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 } $ / ) ) ;
2220
2321export function register ( config ) {
2422 if ( process . env . NODE_ENV === "production" && "serviceWorker" in navigator ) {
@@ -43,10 +41,8 @@ export function register(config) {
4341 // Add some additional logging to localhost, pointing developers to the
4442 // service worker/PWA documentation.
4543 navigator . serviceWorker . ready . then ( ( ) => {
46- console . log (
47- "This web app is being served cache-first by a service " +
48- "worker. To learn more, visit https://bit.ly/CRA-PWA"
49- ) ;
44+ console . log ( "This web app is being served cache-first by a service " +
45+ "worker. To learn more, visit https://bit.ly/CRA-PWA" ) ;
5046 } ) ;
5147 } else {
5248 // Is not localhost. Just register service worker
@@ -57,88 +53,77 @@ export function register(config) {
5753}
5854
5955function registerValidSW ( swUrl , config ) {
60- navigator . serviceWorker
61- . register ( swUrl )
62- . then ( ( registration ) => {
63- registration . onupdatefound = ( ) => {
64- const installingWorker = registration . installing ;
65- if ( installingWorker == null ) {
66- return ;
67- }
68- installingWorker . onstatechange = ( ) => {
69- if ( installingWorker . state === "installed" ) {
70- if ( navigator . serviceWorker . controller ) {
71- // At this point, the updated precached content has been
72- // fetched, but the previous service worker will still serve the
73- // older content until all client tabs are closed.
74- console . log (
75- "New content is available and will be used when all " +
76- "tabs for this page are closed. See https://bit.ly/CRA-PWA."
77- ) ;
56+ navigator . serviceWorker . register ( swUrl )
57+ . then ( ( registration ) => {
58+ registration . onupdatefound = ( ) => {
59+ const installingWorker = registration . installing ;
60+ if ( installingWorker == null ) {
61+ return ;
62+ }
63+ installingWorker . onstatechange = ( ) => {
64+ if ( installingWorker . state === "installed" ) {
65+ if ( navigator . serviceWorker . controller ) {
66+ // At this point, the updated precached content has been
67+ // fetched, but the previous service worker will still serve the
68+ // older content until all client tabs are closed.
69+ console . log (
70+ "New content is available and will be used when all " +
71+ "tabs for this page are closed. See https://bit.ly/CRA-PWA." ) ;
7872
79- // Execute callback
80- if ( config && config . onUpdate ) {
81- config . onUpdate ( registration ) ;
82- }
83- } else {
84- // At this point, everything has been precached.
85- // It's the perfect time to display a
86- // "Content is cached for offline use." message.
87- console . log ( "Content is cached for offline use." ) ;
73+ // Execute callback
74+ if ( config && config . onUpdate ) {
75+ config . onUpdate ( registration ) ;
76+ }
77+ } else {
78+ // At this point, everything has been precached.
79+ // It's the perfect time to display a
80+ // "Content is cached for offline use." message.
81+ console . log ( "Content is cached for offline use." ) ;
8882
89- // Execute callback
90- if ( config && config . onSuccess ) {
91- config . onSuccess ( registration ) ;
83+ // Execute callback
84+ if ( config && config . onSuccess ) {
85+ config . onSuccess ( registration ) ;
86+ }
9287 }
9388 }
94- }
89+ } ;
9590 } ;
96- } ;
97- } )
98- . catch ( ( error ) => {
99- console . error ( "Error during service worker registration:" , error ) ;
100- } ) ;
91+ } )
92+ . catch ( ( error ) => {
93+ console . error ( "Error during service worker registration:" , error ) ;
94+ } ) ;
10195}
10296
10397function checkValidServiceWorker ( swUrl , config ) {
10498 // Check if the service worker can be found. If it can't reload the page.
10599 fetch ( swUrl , {
106- headers : { "Service-Worker" : "script" } ,
100+ headers : { "Service-Worker" : "script" } ,
107101 } )
108- . then ( ( response ) => {
109- // Ensure service worker exists, and that we really are getting a JS
110- // file.
111- const contentType = response . headers . get ( "content-type" ) ;
112- if (
113- response . status === 404 ||
114- ( contentType != null && contentType . indexOf ( "javascript" ) === - 1 )
115- ) {
116- // No service worker found. Probably a different app. Reload the page.
117- navigator . serviceWorker . ready . then ( ( registration ) => {
118- registration . unregister ( ) . then ( ( ) => {
119- window . location . reload ( ) ;
102+ . then ( ( response ) => {
103+ // Ensure service worker exists, and that we really are getting a JS
104+ // file.
105+ const contentType = response . headers . get ( "content-type" ) ;
106+ if ( response . status === 404 ||
107+ ( contentType != null && contentType . indexOf ( "javascript" ) === - 1 ) ) {
108+ // No service worker found. Probably a different app. Reload the page.
109+ navigator . serviceWorker . ready . then ( ( registration ) => {
110+ registration . unregister ( ) . then ( ( ) => { window . location . reload ( ) ; } ) ;
120111 } ) ;
121- } ) ;
122- } else {
123- // Service worker found. Proceed as normal.
124- registerValidSW ( swUrl , config ) ;
125- }
126- } )
127- . catch ( ( ) => {
128- console . log (
129- "No internet connection found. App is running in offline mode."
130- ) ;
131- } ) ;
112+ } else {
113+ // Service worker found. Proceed as normal.
114+ registerValidSW ( swUrl , config ) ;
115+ }
116+ } )
117+ . catch ( ( ) => {
118+ console . log (
119+ "No internet connection found. App is running in offline mode." ) ;
120+ } ) ;
132121}
133122
134123export function unregister ( ) {
135124 if ( "serviceWorker" in navigator ) {
136125 navigator . serviceWorker . ready
137- . then ( ( registration ) => {
138- registration . unregister ( ) ;
139- } )
140- . catch ( ( error ) => {
141- console . error ( error . message ) ;
142- } ) ;
126+ . then ( ( registration ) => { registration . unregister ( ) ; } )
127+ . catch ( ( error ) => { console . error ( error . message ) ; } ) ;
143128 }
144129}
0 commit comments