@@ -18,7 +18,7 @@ const isLocalhost = Boolean(
1818 )
1919) ;
2020
21- export default function register ( swUrl ) {
21+ export default function register ( swUrl , sendNotification ) {
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 ) ;
@@ -32,7 +32,7 @@ export default function register(swUrl) {
3232 window . addEventListener ( 'load' , ( ) => {
3333 if ( ! isLocalhost ) {
3434 // Is not local host. Just register service worker
35- registerValidSW ( swUrl ) ;
35+ registerValidSW ( swUrl , sendNotification ) ;
3636 } else {
3737 // This is running on localhost. Lets check if a service worker still exists or not.
3838 checkValidServiceWorker ( swUrl ) ;
@@ -41,7 +41,7 @@ export default function register(swUrl) {
4141 }
4242}
4343
44- function registerValidSW ( swUrl ) {
44+ function registerValidSW ( swUrl , sendNotification ) {
4545 navigator . serviceWorker
4646 . register ( swUrl )
4747 . then ( registration => {
@@ -55,11 +55,27 @@ function registerValidSW(swUrl) {
5555 // It's the perfect time to display a "New content is
5656 // available; please refresh." message in your web app.
5757 console . log ( 'New content is available; please refresh.' ) ;
58+
59+ if ( sendNotification ) {
60+ sendNotification (
61+ 'CodeSandbox received an update, refresh to see it!' ,
62+ 'notice'
63+ ) ;
64+ } else {
65+ document . location . reload ( ) ;
66+ }
5867 } else {
5968 // At this point, everything has been precached.
6069 // It's the perfect time to display a
6170 // "Content is cached for offline use." message.
6271 console . log ( 'Content is cached for offline use.' ) ;
72+
73+ if ( sendNotification ) {
74+ sendNotification (
75+ 'CodeSandbox has been cached, it now works offline.' ,
76+ 'notice'
77+ ) ;
78+ }
6379 }
6480 }
6581 } ;
0 commit comments