Commit d6754918 authored by cgm616's avatar cgm616

Add offline fallback

TODO: this needs to be expanded to work with images and other stuff.
parent f88c65c7
---
layout: layout.html
---
## Uh oh!
You are currently offline, or have no connection to the server for this website!
Thanks to the service worker you'll still have access to the pages you've visited, but the one you currently tried to find either doesn't exist or has not yet been cached.
Sorry :/
......@@ -7,7 +7,8 @@ self.addEventListener('install', function(event) {
return cache.addAll([
'/js/app.js',
'/css/main.css',
'/css/highlight-default.css'
'/css/highlight-default.css',
'/offline/index.html'
]);
}).then(function() {
console.log("[install] cache preloaded");
......@@ -16,12 +17,12 @@ self.addEventListener('install', function(event) {
);
});
function promiseAny(promises) {
function promiseAny(promises, fallback) {
return new Promise((resolve, reject) => {
promises = promises.map(p => Promise.resolve(p));
promises.forEach(p => p.then(resolve));
promises.reduce((a, b) => a.catch(() => b))
.catch(() => reject(Error("All failed")));
.catch(() => resolve(fallback));
});
};
......@@ -46,7 +47,7 @@ self.addEventListener('fetch', function(event) {
return Promise.reject('network request failed');
}
}),
]);
], cache.match("/offline/index.html"));
})
);
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment