Skip to content

Commit 55d6c27

Browse files
author
Ives van Hoorne
committed
Show notices for service worker updates
1 parent 198c933 commit 55d6c27

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/app/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ThemeProvider } from 'styled-components';
66
import { ConnectedRouter } from 'react-router-redux';
77
import registerServiceWorker from 'common/registerServiceWorker';
88
import 'normalize.css';
9+
import notificationActions from 'app/store/notifications/actions';
910

1011
import App from './pages/index';
1112
import '../common/global.css';
@@ -15,8 +16,6 @@ import theme from '../common/theme';
1516
import logError from './utils/error';
1617
import history from './utils/history';
1718

18-
registerServiceWorker('/service-worker.js');
19-
2019
if (process.env.NODE_ENV === 'production') {
2120
try {
2221
Raven.config('https://[email protected]/155188', {
@@ -69,6 +68,12 @@ if (process.env.NODE_ENV === 'production') {
6968
const rootEl = document.getElementById('root');
7069

7170
const store = createStore(history);
71+
72+
const showNotification = (message, type) =>
73+
store.dispatch(notificationActions.addNotification(message, type));
74+
75+
registerServiceWorker('/service-worker.js', showNotification);
76+
7277
const renderApp = RootComponent => {
7378
try {
7479
render(

src/common/registerServiceWorker.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)