Skip to content

Commit

Permalink
Merge pull request vinitshahdeo#86 from UddeshJain/feature/frontend
Browse files Browse the repository at this point in the history
Making PWA
  • Loading branch information
jainpawan21 authored Mar 4, 2020
2 parents 2684336 + a41675b commit f239fa2
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 9 deletions.
14 changes: 13 additions & 1 deletion react-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions react-frontend/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,20 @@
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('worker.js').then(function(registration) {
console.log('Worker registration successful', registration.scope);
}, function(err) {
console.log('Worker registration failed', err);
}).catch(function(err) {
console.log(err);
});
});
} else {
console.log('Service Worker is not supported by browser.');
}
</script>
</body>
</html>
4 changes: 2 additions & 2 deletions react-frontend/public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"short_name": "Water-monitoring-system",
"name": "Water-monitoring-system",
"icons": [
{
"src": "favicon.ico",
Expand Down
51 changes: 51 additions & 0 deletions react-frontend/public/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var CACHE_NAME = 'water-monitoring-system';
var urlsToCache = [
'/'
];

// Install a service worker
self.addEventListener('install', event => {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
.catch(err => console.error(err))
);
});

// Cache and return requests
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(function(response) {
// Cache hit - return response
if (response) {
return response;
}
return fetch(event.request);
}
)
.catch(err => console.error(err))
);
});

// Update a service worker
self.addEventListener('activate', event => {
var cacheWhitelist = ['uddesh-portfolio-pwa'];
event.waitUntil(
caches.keys()
.then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
.catch(err => console.error(err))
);
});
2 changes: 1 addition & 1 deletion react-frontend/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ ReactDOM.render(<App />, document.getElementById('root'));
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
serviceWorker.register();
12 changes: 7 additions & 5 deletions react-frontend/src/pages/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class Home extends Component {
</center>
</div>
<div className="row m-5 d-flex justify-content-center align-items-center">
{this.props.tanks.map(tank=> (
<div>
{this.props.tanks.map((tank, index)=> (
<div key={index}>
<div className="col-lg-3 d-flex align-items-center" id={tank.id}>
<center>
<TankCircle>
Expand Down Expand Up @@ -50,9 +50,11 @@ class Home extends Component {
</div>
</div>
<hr/>
<p>
<center><strong>Made by Vinit Shahdeo</strong></center>
</p>
<center>
<p>
<strong>Made by Vinit Shahdeo</strong>
</p>
</center>
</div>
</div>
)
Expand Down

0 comments on commit f239fa2

Please sign in to comment.