Skip to content

Commit

Permalink
Update service worker
Browse files Browse the repository at this point in the history
- Respond with the last updated cache to network first
- Then make a network call and update the cache
- https://jakearchibald.com/2014/offline-cookbook/#stale-while-revalidate
- This means for any change to reflect there will be two refresh needed
- Fixes #5
  • Loading branch information
mitul45 committed May 15, 2017
1 parent d1c0ab7 commit d7c0de5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
1 change: 0 additions & 1 deletion script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Constants

const CLIENT_ID =
"840179112792-bhg3k1h0dcnp9ltelj21o6vibphjcufe.apps.googleusercontent.com";
const DISCOVERY_DOCS = [
Expand Down
24 changes: 16 additions & 8 deletions sw.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var CACHE_NAME = "expense-manager-cache";
var urlsToCache = [
// "style.css",
"style.css",
"icons/favicon-32x32.png",
"icons/favicon-16x16.png",
// "script.js",
"script.js",
"vendor/mdl/material.min.js",
"vendor/mdl/material.min.css"
];
Expand All @@ -21,13 +21,21 @@ self.addEventListener("install", function(event) {

// listen for fetch events
self.addEventListener("fetch", function(event) {
const requestURL = new URL(event.request.url);
event.respondWith(
caches.match(event.request).then(function(response) {
// Cache hit - return response
if (response) {
return response;
}
return fetch(event.request);
caches.open(CACHE_NAME).then(function(cache) {
return caches.match(event.request).then(function(response) {
var fetchPromise = fetch(event.request).then(function(networkResponse) {
// cache same host files only
if (
requestURL.pathname === "mitul45.github.io" ||
requestURL.hostname === "localhost"
)
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
return response || fetchPromise;
});
})
);
});

0 comments on commit d7c0de5

Please sign in to comment.