Declaritively initializing service worker

This commit is contained in:
Jeff Avallone 2018-02-11 10:29:35 -05:00
parent fd5a8786d0
commit 6bf094a4c1
2 changed files with 19 additions and 12 deletions

View File

@ -4,9 +4,12 @@ import ReactDOM from 'react-dom';
import App from '../../components/App'; import App from '../../components/App';
import '../../style.css'; import '../../style.css';
import '../../service-worker'; import { setupServiceWorker } from '../../service-worker';
import { setupGA } from '../../analytics'; import { setupGA } from '../../analytics';
if (process.env.NODE_ENV === 'production') {
setupServiceWorker();
}
setupGA(); setupGA();
ReactDOM.render(<App/>, document.getElementById('root')); ReactDOM.render(<App/>, document.getElementById('root'));

View File

@ -1,4 +1,5 @@
if ('serviceWorker' in navigator && process.env.NODE_ENV === 'production') { const setupServiceWorker = () => {
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => { window.addEventListener('load', () => {
navigator.serviceWorker.register('/sw.js') navigator.serviceWorker.register('/sw.js')
.then(registration => { .then(registration => {
@ -9,3 +10,6 @@ if ('serviceWorker' in navigator && process.env.NODE_ENV === 'production') {
}); });
}); });
} }
};
export { setupServiceWorker };