snapdrop

A Progressive Web App for local file sharing
git clone http://git.hanabi.in/repos/snapdrop.git
Log | Files | Refs | README | LICENSE

commit 2cd1798b15bba7dd0ca0fb5b750bb9f2212736ab
parent 73c7430883a92d2f81edbf98d7836d16f8c4457e
Author: RobinLinus <robinlinus@users.noreply.github.com>
Date:   Wed, 24 Oct 2018 17:43:50 +0200

Add ServiceWorker implementation

Diffstat:
Mclient/scripts/ui.js | 12+++++++++---
Mclient/service-worker.js | 34++++++++++++++++++++++++++++++++++
2 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/client/scripts/ui.js b/client/scripts/ui.js @@ -399,7 +399,7 @@ class Notifications { _copyText(message, notification) { notification.close(); - if(!document.copy(message)) return; + if (!document.copy(message)) return; this._notify('Copied text to clipboard'); } @@ -463,14 +463,20 @@ document.copy = text => { } -if ('serviceWorker' in navigator && !window.iOS) { - // SW on iOS is buggy. see: https://stackoverflow.com/questions/18103103/failed-to-load-resource-plugin-handled-load-on-ios +if ('serviceWorker' in navigator) { navigator.serviceWorker .register('/service-worker.js') .then(serviceWorker => { console.log('Service Worker registered'); window.serviceWorker = serviceWorker }); + + // don't display install banner when installed + window.addEventListener('beforeinstallprompt', e => { + if (window.matchMedia('(display-mode: standalone)').matches) { + return event.preventDefault(); + } + }); } // Background Animation diff --git a/client/service-worker.js b/client/service-worker.js @@ -0,0 +1,34 @@ +var CACHE_NAME = 'my-site-cache-v1'; +var urlsToCache = [ + '/', + '/styles.css', + '/scripts/network.js', + '/scripts/ui.js', + '/sounds/blop.mp3' +]; + +self.addEventListener('install', function(event) { + // Perform install steps + event.waitUntil( + caches.open(CACHE_NAME) + .then(function(cache) { + console.log('Opened cache'); + return cache.addAll(urlsToCache); + }) + ); +}); + + +self.addEventListener('fetch', function(event) { + event.respondWith( + caches.match(event.request) + .then(function(response) { + // Cache hit - return response + if (response) { + return response; + } + return fetch(event.request); + } + ) + ); +});