Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add manifest.json and cache static assets for offline use #331

Merged
merged 8 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions examples/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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();
});
};
8 changes: 6 additions & 2 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 @@ -83,8 +84,11 @@ <h1>Image Sequencer</h1>
</div>
</section>
<button class="btn btn-primary btn-lg" name="save-sequence" id="save-seq">Save Sequence</button>

</div>

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

<script type="text/javascript">
$(function() {
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.