From 0a60570aaf54138b88a115eaa30bd1b6f8422097 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 19 Jul 2018 19:02:09 +0200 Subject: Add offline support --- appcache.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 appcache.js (limited to 'appcache.js') diff --git a/appcache.js b/appcache.js new file mode 100644 index 0000000..87d5e80 --- /dev/null +++ b/appcache.js @@ -0,0 +1,60 @@ +/* + * appcache.js + * Part of Pointless Viewer, a Beamer presentation viewer + * Copyright 2018 Pacien TRAN-GIRARD + * License: GNU GPL v3 + */ + +"use strict"; + +class AppCache { + constructor() { + this.cacheName = "cache"; + this.filesToCache = [ + "viewer.html", + "appcache.js", + + "pointless/pdfjs/pdf.js", + "pointless/pdfjs/pdf.worker.js", + + "pointless/viewer/viewer.css", + "pointless/viewer/init.js", + "pointless/viewer/viewer.js", + "pointless/viewer/presentation.js", + "pointless/viewer/stage.js", + "pointless/viewer/screen.js", + "pointless/viewer/timer.js" + ]; + + var appCache = this; + self.addEventListener("install", function(event) { + event.waitUntil(appCache._onInstall()); + }); + + self.addEventListener("fetch", function(event) { + event.respondWith(appCache._onFetch(event.request)); + }); + } + + _onInstall() { + var self = this; + return caches.open(this.cacheName).then(function(cache) { + return cache.addAll(self.filesToCache); + }); + } + + _onFetch(request) { + return navigator.onLine ? this._fetchUpdate(request) : caches.match(request); + } + + _fetchUpdate(request) { + return caches.open(this.cacheName).then(function(cache) { + return fetch(request).then(function(response) { + cache.put(request, response.clone()); + return response + }); + }); + } +} + +var appCache = new AppCache(); -- cgit v1.2.3