summaryrefslogtreecommitdiff
path: root/worker.js
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2017-02-26 00:14:30 +0300
committerAndrew Dolgov <[email protected]>2017-02-26 00:14:30 +0300
commitb8ae4b31c129031e89e4c7fb9801d6761513dd73 (patch)
tree4522dc5c059837a8c143c2a90c1dc562db0915ff /worker.js
parentf858879a3d13bcec694a5e2493fbfe3a1a233e13 (diff)
experimental service worker offline stuff
Diffstat (limited to 'worker.js')
-rw-r--r--worker.js72
1 files changed, 72 insertions, 0 deletions
diff --git a/worker.js b/worker.js
new file mode 100644
index 0000000..2ef4b9c
--- /dev/null
+++ b/worker.js
@@ -0,0 +1,72 @@
+var CACHE_NAME = 'epube-test';
+
+self.addEventListener('activate', function(event) {
+ event.waitUntil(
+ caches.open(CACHE_NAME).then(function(cache) {
+ var urls = [
+ 'read.html',
+ 'js/read.js',
+ 'js/offline.js',
+ 'css/read.css',
+ 'offline.html',
+ 'lib/zip.min.js',
+ 'lib/epub.js',
+ 'lib/localforage.min.js',
+ 'lib/holder.min.js',
+ 'lib/smartimages.js',
+ 'lib/bootstrap/v3/css/bootstrap-theme.min.css',
+ 'lib/bootstrap/v3/css/bootstrap.min.css',
+ 'lib/bootstrap/v3/js/jquery.js',
+ 'lib/bootstrap/v3/js/bootstrap.min.js',
+ 'lib/bootstrap/v3/fonts/glyphicons-halflings-regular.woff2',
+ ];
+
+ return cache.addAll(urls.map(url => new Request(url, {credentials: 'same-origin'})));
+ })
+ );
+});
+
+this.addEventListener('fetch', function(event) {
+ var req = event.request.clone();
+
+ event.respondWith(
+ caches.match(req).then(function(resp) {
+ if (!navigator.onLine) {
+
+ if (resp) return resp;
+
+ console.log(req.url);
+
+ if (req.url.match("read.html")) {
+ return caches.match("read.html");
+ }
+
+ if (req.url.match("index.php")) {
+ return caches.match("offline.html");
+ }
+ }
+
+ return fetch(req)
+ .then(function(resp) {
+
+ if (req.url.match(/(getlastread|getpagination|\.epub)/)) {
+ caches.open(CACHE_NAME).then(function(cache) {
+ cache.put(event.request, resp.clone());
+ });
+ } /*else {
+ caches.match(req.url).then(function(cached) {
+ if (cached) {
+ console.log('refreshing ' + req.url);
+
+ caches.open(CACHE_NAME).then(function(cache) {
+ cache.put(event.request, resp.clone());
+ });
+ }
+ });
+ } */
+
+ return resp.clone();
+ });
+ })
+ );
+});