From 98c71b18a8c86646f0cdf95c11e1e3b2a8d36edb Mon Sep 17 00:00:00 2001 From: Jeffrey Warren Date: Tue, 20 Nov 2018 22:53:38 -0500 Subject: [PATCH] Add manifest.json and cache static assets for offline use (#331) (#486) * Add manifest.json * cache static assets for offline use * update cache * add meta theme-color and change static files to be cache * cache the files on network request * caching on first run Signed-off-by: tech4GT * add a button to clear cache * add styling to clear cache link --- examples/demo.js | 36 ++++++++++++++++++++++++++++++++++++ examples/index.html | 15 +++++++++++---- examples/manifest.json | 21 +++++++++++++++++++++ examples/sw.js | 35 +++++++++++++++++++++++++++++++++++ icons/ic_512.png | Bin 0 -> 48895 bytes 5 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 examples/manifest.json create mode 100644 examples/sw.js create mode 100644 icons/ic_512.png diff --git a/examples/demo.js b/examples/demo.js index 1a901da9..7f1024ab 100644 --- a/examples/demo.js +++ b/examples/demo.js @@ -132,4 +132,40 @@ window.onload = function() { step.options.step.imgElement.src = reader.result; } }); + + if ('serviceWorker' in navigator) { + navigator.serviceWorker.register('sw.js', { scope: '/examples/' }) + .then(function(registration) { + const installingWorker = registration.installing; + installingWorker.onstatechange = () => { + console.log(installingWorker) + if (installingWorker.state === 'installed') { + location.reload(); + } + } + console.log('Registration successful, scope is:', registration.scope); + }) + .catch(function(error) { + console.log('Service worker registration failed, error:', error); + }); + } + + if ('serviceWorker' in navigator) { + caches.keys().then(function(cacheNames) { + cacheNames.forEach(function(cacheName) { + $("#clear-cache").append(" " + cacheName); + }); + }); + } + + $("#clear-cache").click(function() { + if ('serviceWorker' in navigator) { + caches.keys().then(function(cacheNames) { + cacheNames.forEach(function(cacheName) { + caches.delete(cacheName); + }); + }); + } + location.reload(); + }); }; diff --git a/examples/index.html b/examples/index.html index 4cd7219d..3e4144d0 100644 --- a/examples/index.html +++ b/examples/index.html @@ -7,8 +7,9 @@ + - + Image Sequencer @@ -123,10 +124,16 @@ - -
+ + + -
+ + + +