From 15513ed4dde4cd8531ff917c2b9c1c2e05ed319f Mon Sep 17 00:00:00 2001 From: Mridul97 Date: Fri, 26 Oct 2018 23:28:26 +0530 Subject: [PATCH] Add manifest.json and cache static assets for offline use (#331) * 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 | 42 ++++++++++++++++++++++++++++++++++++++--- examples/index.html | 8 ++++++-- examples/manifest.json | 21 +++++++++++++++++++++ examples/sw.js | 35 ++++++++++++++++++++++++++++++++++ icons/ic_512.png | Bin 0 -> 48895 bytes 5 files changed, 101 insertions(+), 5 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 f610fe3b..086183aa 100644 --- a/examples/demo.js +++ b/examples/demo.js @@ -28,10 +28,10 @@ window.onload = function() { $("#addStep select").on("change", ui.selectNewStepUi); $("#addStep #add-step-btn").on("click", ui.addStepUi); $('#addStep #download-btn').click(function() { - $('img:last()').trigger( "click" ); - + $('img:last()').trigger("click"); + return false; - }); + }); $('body').on('click', 'button.remove', ui.removeStepUi); $('#save-seq').click(() => { sequencer.saveSequence(window.prompt("Please give a name to your sequence..."), sequencer.toString()); @@ -51,4 +51,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 e2cbd634..e07dce5d 100644 --- a/examples/index.html +++ b/examples/index.html @@ -7,8 +7,9 @@ + - + Image Sequencer @@ -83,8 +84,11 @@ - + +