Skip to content

Commit

Permalink
Added Service Worker
Browse files Browse the repository at this point in the history
  • Loading branch information
unigazer committed May 1, 2018
1 parent b25dc13 commit a29ef83
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Cache name
var CACHE = 'moonly';

// Cache all of the files upong install event
addEventListener('install', e => {
e.waitUntil(
caches.open(CACHE).then(cache => {
return cache.addAll([
'//fonts.googleapis.com/icon?family=Material+Icons',
'//fonts.googleapis.com/css?family=Lato:300,400,700,900',
'../css/bootstrap.min.css',
'../css/fontawesome/css/font-awesome.css',
'../css/style.css',
'../css/cat-colors.css',
'../js/libs/chrome-promise.js',
'../js/libs/jquery.min.js',
'../js/libs/bootstrap.min.js',
'../js/libs/moment.js',
'../js/libs/sha256.min.js',
'../js/libs/sockjs.min.js',
'../js/config.js',
'../js/popup/query.js',
'../js/newtab/jquery.nicescroll.js',
'../js/newtab/loginTest.js',
'../js/newtab/script.js'
]);
})
);
});

// Fetch from cache first, then update the cache from network
addEventListener('fetch', e => {
e.respondWith(fromCache(e.request));
e.waitUntil(updateCache(e.request));
})

// Load from cache
let fromCache = req => {
return caches.open(CACHE).then(cache => {
return cache.match(req).then(res => {
return res || Promise.reject('no-match');
});
});
};

// Update the cache from network
let updateCache = req => {
return caches.open(CACHE).then(cache => {
return fetch(req).then(res => {
return cache.put(request, res);
});
});
};
11 changes: 10 additions & 1 deletion src/views/newtab.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,16 @@ <h2>Sign In</h2>

<script src='../js/newtab/loginTest.js'></script>
<script src='../js/newtab/script.js'></script>
<!-- Service Worker -->
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('../service-worker.js', {scope: '../newtab.html'})
.catch(err => {
console.error(`Failed to register Service Worker, ${err.message}`);
});
};
</script>

</body>

</html>
</html>

0 comments on commit a29ef83

Please sign in to comment.