@@ -18,7 +18,7 @@ const isLocalhost = Boolean(
18
18
)
19
19
) ;
20
20
21
- export function register ( ) {
21
+ export function register ( config ) {
22
22
if ( process . env . NODE_ENV === 'production' && 'serviceWorker' in navigator ) {
23
23
// The URL constructor is available in all browsers that support SW.
24
24
const publicUrl = new URL ( process . env . PUBLIC_URL , window . location ) ;
@@ -34,7 +34,7 @@ export function register() {
34
34
35
35
if ( isLocalhost ) {
36
36
// This is running on localhost. Lets check if a service worker still exists or not.
37
- checkValidServiceWorker ( swUrl ) ;
37
+ checkValidServiceWorker ( swUrl , config ) ;
38
38
39
39
// Add some additional logging to localhost, pointing developers to the
40
40
// service worker/PWA documentation.
@@ -46,13 +46,13 @@ export function register() {
46
46
} ) ;
47
47
} else {
48
48
// Is not local host. Just register service worker
49
- registerValidSW ( swUrl ) ;
49
+ registerValidSW ( swUrl , config ) ;
50
50
}
51
51
} ) ;
52
52
}
53
53
}
54
54
55
- function registerValidSW ( swUrl ) {
55
+ function registerValidSW ( swUrl , config ) {
56
56
navigator . serviceWorker
57
57
. register ( swUrl )
58
58
. then ( registration => {
@@ -66,11 +66,21 @@ function registerValidSW(swUrl) {
66
66
// It's the perfect time to display a "New content is
67
67
// available; please refresh." message in your web app.
68
68
console . log ( 'New content is available; please refresh.' ) ;
69
+
70
+ // Execute callback
71
+ if ( config . onUpdate ) {
72
+ config . onUpdate ( registration ) ;
73
+ }
69
74
} else {
70
75
// At this point, everything has been precached.
71
76
// It's the perfect time to display a
72
77
// "Content is cached for offline use." message.
73
78
console . log ( 'Content is cached for offline use.' ) ;
79
+
80
+ // Execute callback
81
+ if ( config . onSuccess ) {
82
+ config . onSuccess ( registration ) ;
83
+ }
74
84
}
75
85
}
76
86
} ;
@@ -81,7 +91,7 @@ function registerValidSW(swUrl) {
81
91
} ) ;
82
92
}
83
93
84
- function checkValidServiceWorker ( swUrl ) {
94
+ function checkValidServiceWorker ( swUrl , config ) {
85
95
// Check if the service worker can be found. If it can't reload the page.
86
96
fetch ( swUrl )
87
97
. then ( response => {
@@ -98,7 +108,7 @@ function checkValidServiceWorker(swUrl) {
98
108
} ) ;
99
109
} else {
100
110
// Service worker found. Proceed as normal.
101
- registerValidSW ( swUrl ) ;
111
+ registerValidSW ( swUrl , config ) ;
102
112
}
103
113
} )
104
114
. catch ( ( ) => {
0 commit comments