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 =
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 } $ / ) ) ;
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+ ) ;
2022
2123export function register ( config ) {
2224 if ( process . env . NODE_ENV === "production" && "serviceWorker" in navigator ) {
@@ -41,8 +43,10 @@ export function register(config) {
4143 // Add some additional logging to localhost, pointing developers to the
4244 // service worker/PWA documentation.
4345 navigator . serviceWorker . ready . then ( ( ) => {
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" ) ;
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+ ) ;
4650 } ) ;
4751 } else {
4852 // Is not localhost. Just register service worker
@@ -53,77 +57,88 @@ export function register(config) {
5357}
5458
5559function registerValidSW ( swUrl , config ) {
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." ) ;
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+ ) ;
7278
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." ) ;
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." ) ;
8288
83- // Execute callback
84- if ( config && config . onSuccess ) {
85- config . onSuccess ( registration ) ;
86- }
89+ // Execute callback
90+ if ( config && config . onSuccess ) {
91+ config . onSuccess ( registration ) ;
8792 }
8893 }
89- } ;
94+ }
9095 } ;
91- } )
92- . catch ( ( error ) => {
93- console . error ( "Error during service worker registration:" , error ) ;
94- } ) ;
96+ } ;
97+ } )
98+ . catch ( ( error ) => {
99+ console . error ( "Error during service worker registration:" , error ) ;
100+ } ) ;
95101}
96102
97103function checkValidServiceWorker ( swUrl , config ) {
98104 // Check if the service worker can be found. If it can't reload the page.
99105 fetch ( swUrl , {
100- headers : { "Service-Worker" : "script" } ,
106+ headers : { "Service-Worker" : "script" } ,
101107 } )
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 ( ) ; } ) ;
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 ( ) ;
111120 } ) ;
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- } ) ;
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+ } ) ;
121132}
122133
123134export function unregister ( ) {
124135 if ( "serviceWorker" in navigator ) {
125136 navigator . serviceWorker . ready
126- . then ( ( registration ) => { registration . unregister ( ) ; } )
127- . catch ( ( error ) => { console . error ( error . message ) ; } ) ;
137+ . then ( ( registration ) => {
138+ registration . unregister ( ) ;
139+ } )
140+ . catch ( ( error ) => {
141+ console . error ( error . message ) ;
142+ } ) ;
128143 }
129144}
0 commit comments