1+ // tslint:disable:no-console
12// In production, we register a service worker to serve assets from local cache.
23
34// This lets the app load faster on subsequent visits in production, and gives
45// it offline capabilities. However, it also means that developers (and users)
5- // will only see deployed updates on the " N+1" visit to a page, since previously
6+ // will only see deployed updates on the ' N+1' visit to a page, since previously
67// cached resources are updated in the background.
78
89// To learn more about the benefits of this model, read https://goo.gl/KwvDNy.
@@ -21,7 +22,10 @@ const isLocalhost = Boolean(
2122export default function register ( ) {
2223 if ( process . env . NODE_ENV === 'production' && 'serviceWorker' in navigator ) {
2324 // The URL constructor is available in all browsers that support SW.
24- const publicUrl = new URL ( process . env . PUBLIC_URL , window . location ) ;
25+ const publicUrl = new URL (
26+ process . env . PUBLIC_URL ,
27+ window . location . toString ( )
28+ ) ;
2529 if ( publicUrl . origin !== window . location . origin ) {
2630 // Our service worker won't work if PUBLIC_URL is on a different origin
2731 // from what our page is served on. This might happen if a CDN is used to
@@ -43,43 +47,45 @@ export default function register() {
4347 }
4448}
4549
46- function registerValidSW ( swUrl ) {
50+ function registerValidSW ( swUrl : string ) {
4751 navigator . serviceWorker
4852 . register ( swUrl )
4953 . then ( registration => {
5054 registration . onupdatefound = ( ) => {
5155 const installingWorker = registration . installing ;
52- installingWorker . onstatechange = ( ) => {
53- if ( installingWorker . state === 'installed' ) {
54- if ( navigator . serviceWorker . controller ) {
55- // At this point, the old content will have been purged and
56- // the fresh content will have been added to the cache.
57- // It's the perfect time to display a "New content is
58- // available; please refresh." message in your web app.
59- console . log ( 'New content is available; please refresh.' ) ;
60- } else {
61- // At this point, everything has been precached.
62- // It's the perfect time to display a
63- // "Content is cached for offline use." message.
64- console . log ( 'Content is cached for offline use.' ) ;
56+ if ( installingWorker ) {
57+ installingWorker . onstatechange = ( ) => {
58+ if ( installingWorker . state === 'installed' ) {
59+ if ( navigator . serviceWorker . controller ) {
60+ // At this point, the old content will have been purged and
61+ // the fresh content will have been added to the cache.
62+ // It's the perfect time to display a 'New content is
63+ // available; please refresh.' message in your web app.
64+ console . log ( 'New content is available; please refresh.' ) ;
65+ } else {
66+ // At this point, everything has been precached.
67+ // It's the perfect time to display a
68+ // 'Content is cached for offline use.' message.
69+ console . log ( 'Content is cached for offline use.' ) ;
70+ }
6571 }
66- }
67- } ;
72+ } ;
73+ }
6874 } ;
6975 } )
7076 . catch ( error => {
7177 console . error ( 'Error during service worker registration:' , error ) ;
7278 } ) ;
7379}
7480
75- function checkValidServiceWorker ( swUrl ) {
81+ function checkValidServiceWorker ( swUrl : string ) {
7682 // Check if the service worker can be found. If it can't reload the page.
7783 fetch ( swUrl )
7884 . then ( response => {
7985 // Ensure service worker exists, and that we really are getting a JS file.
8086 if (
8187 response . status === 404 ||
82- response . headers . get ( 'content-type' ) . indexOf ( 'javascript' ) === - 1
88+ response . headers . get ( 'content-type' ) ! . indexOf ( 'javascript' ) === - 1
8389 ) {
8490 // No service worker found. Probably a different app. Reload the page.
8591 navigator . serviceWorker . ready . then ( registration => {
0 commit comments