@@ -18,7 +18,7 @@ const isLocalhost = Boolean(
1818 )
1919) ;
2020
21- export function register ( ) {
21+ export function register ( config ) {
2222 if ( process . env . NODE_ENV === 'production' && 'serviceWorker' in navigator ) {
2323 // The URL constructor is available in all browsers that support SW.
2424 const publicUrl = new URL ( process . env . PUBLIC_URL , window . location ) ;
@@ -34,7 +34,7 @@ export function register() {
3434
3535 if ( isLocalhost ) {
3636 // This is running on localhost. Lets check if a service worker still exists or not.
37- checkValidServiceWorker ( swUrl ) ;
37+ checkValidServiceWorker ( swUrl , config ) ;
3838
3939 // Add some additional logging to localhost, pointing developers to the
4040 // service worker/PWA documentation.
@@ -46,13 +46,13 @@ export function register() {
4646 } ) ;
4747 } else {
4848 // Is not local host. Just register service worker
49- registerValidSW ( swUrl ) ;
49+ registerValidSW ( swUrl , config ) ;
5050 }
5151 } ) ;
5252 }
5353}
5454
55- function registerValidSW ( swUrl ) {
55+ function registerValidSW ( swUrl , config ) {
5656 navigator . serviceWorker
5757 . register ( swUrl )
5858 . then ( registration => {
@@ -66,11 +66,21 @@ function registerValidSW(swUrl) {
6666 // It's the perfect time to display a "New content is
6767 // available; please refresh." message in your web app.
6868 console . log ( 'New content is available; please refresh.' ) ;
69+
70+ // Execute callback
71+ if ( config . onUpdate ) {
72+ config . onUpdate ( registration ) ;
73+ }
6974 } else {
7075 // At this point, everything has been precached.
7176 // It's the perfect time to display a
7277 // "Content is cached for offline use." message.
7378 console . log ( 'Content is cached for offline use.' ) ;
79+
80+ // Execute callback
81+ if ( config . onSuccess ) {
82+ config . onSuccess ( registration ) ;
83+ }
7484 }
7585 }
7686 } ;
@@ -81,7 +91,7 @@ function registerValidSW(swUrl) {
8191 } ) ;
8292}
8393
84- function checkValidServiceWorker ( swUrl ) {
94+ function checkValidServiceWorker ( swUrl , config ) {
8595 // Check if the service worker can be found. If it can't reload the page.
8696 fetch ( swUrl )
8797 . then ( response => {
@@ -98,7 +108,7 @@ function checkValidServiceWorker(swUrl) {
98108 } ) ;
99109 } else {
100110 // Service worker found. Proceed as normal.
101- registerValidSW ( swUrl ) ;
111+ registerValidSW ( swUrl , config ) ;
102112 }
103113 } )
104114 . catch ( ( ) => {
0 commit comments