1
+ // tslint:disable:no-console
1
2
// In production, we register a service worker to serve assets from local cache.
2
3
3
4
// This lets the app load faster on subsequent visits in production, and gives
4
5
// 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
6
7
// cached resources are updated in the background.
7
8
8
9
// To learn more about the benefits of this model, read https://goo.gl/KwvDNy.
@@ -21,7 +22,10 @@ const isLocalhost = Boolean(
21
22
export default function register ( ) {
22
23
if ( process . env . NODE_ENV === 'production' && 'serviceWorker' in navigator ) {
23
24
// 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
+ ) ;
25
29
if ( publicUrl . origin !== window . location . origin ) {
26
30
// Our service worker won't work if PUBLIC_URL is on a different origin
27
31
// from what our page is served on. This might happen if a CDN is used to
@@ -43,43 +47,45 @@ export default function register() {
43
47
}
44
48
}
45
49
46
- function registerValidSW ( swUrl ) {
50
+ function registerValidSW ( swUrl : string ) {
47
51
navigator . serviceWorker
48
52
. register ( swUrl )
49
53
. then ( registration => {
50
54
registration . onupdatefound = ( ) => {
51
55
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
+ }
65
71
}
66
- }
67
- } ;
72
+ } ;
73
+ }
68
74
} ;
69
75
} )
70
76
. catch ( error => {
71
77
console . error ( 'Error during service worker registration:' , error ) ;
72
78
} ) ;
73
79
}
74
80
75
- function checkValidServiceWorker ( swUrl ) {
81
+ function checkValidServiceWorker ( swUrl : string ) {
76
82
// Check if the service worker can be found. If it can't reload the page.
77
83
fetch ( swUrl )
78
84
. then ( response => {
79
85
// Ensure service worker exists, and that we really are getting a JS file.
80
86
if (
81
87
response . status === 404 ||
82
- response . headers . get ( 'content-type' ) . indexOf ( 'javascript' ) === - 1
88
+ response . headers . get ( 'content-type' ) ! . indexOf ( 'javascript' ) === - 1
83
89
) {
84
90
// No service worker found. Probably a different app. Reload the page.
85
91
navigator . serviceWorker . ready . then ( registration => {
0 commit comments