Skip to content

Commit

Permalink
Add manifest.json and cache static assets for offline use (#331) (#486)
Browse files Browse the repository at this point in the history
* 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 <varun.gupta1798@gmail.com>

* add a button to clear cache

* add styling to clear cache link
  • Loading branch information
jywarren authored Nov 21, 2018
1 parent 3ca4081 commit 98c71b1
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 4 deletions.
36 changes: 36 additions & 0 deletions examples/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
};
15 changes: 11 additions & 4 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="content-type" content="text/html; charset=UTF8">
<meta name="theme-color" content="#428bca">
<link rel="icon" sizes="192x192" href="../icons/ic_192.png">

<link rel="manifest" href="manifest.json">

<title>Image Sequencer</title>

Expand Down Expand Up @@ -123,10 +124,16 @@ <h4 class="modal-title">Your gif is ready</h4>
</div>
</div>
<button class="btn btn-primary btn-lg" name="save-sequence" id="save-seq">Save Sequence</button>
</div>
<form class="move-up" action="#up">
</div>

<form class="move-up" action="#up">
<button><i class="fa fa-arrow-circle-o-up"></i></button>
</form>
</form>

<footer>
<hr style="margin:20px;"><center><a class="color:grey;" id="clear-cache">Clear offline cache</a></center>
</footer>

<script type="text/javascript">
$(function() {
var sequencer;
Expand Down
21 changes: 21 additions & 0 deletions examples/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"short_name": "IS",
"name": "Image Sequencer",
"icons": [
{
"src": "../icons/ic_192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "../icons/ic_512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": "/examples/#steps=",
"background_color": "#428bca",
"display": "standalone",
"scope": "/examples/",
"theme_color": "#428bca"
}
35 changes: 35 additions & 0 deletions examples/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const staticCacheName = 'image-sequencer-static-v3';

self.addEventListener('install', event => {
console.log('Attempting to install service worker');
});

self.addEventListener('activate', function(e) {
console.log('[ServiceWorker] Activate');
e.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName){
return cacheName.startsWith('image-sequencer-') &&
cacheName != staticCacheName;
}).map(function(cacheName){
return caches.delete(cacheName);
})
);
})
);
});

self.addEventListener('fetch', function(event) {
event.respondWith(
caches.open(staticCacheName).then(function(cache) {
return cache.match(event.request).then(function (response) {
return response || fetch(event.request).then(function(response) {
if(event.request.method == "GET")
cache.put(event.request, response.clone());
return response;
});
});
})
);
});
Binary file added icons/ic_512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 98c71b1

Please sign in to comment.