about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-06-30 18:30:33 -0400
committerelioat <elioat@tilde.institute>2024-06-30 18:30:33 -0400
commitf29758cfe8e233fa9d011f3eb7083123bfd40cd5 (patch)
tree8698edceae1f1fb467b533343f1316a53b7cf11d
parentbcdc7c3b2b74cdce311d569c78fc2adee0a66b12 (diff)
downloadtour-f29758cfe8e233fa9d011f3eb7083123bfd40cd5.tar.gz
*
-rw-r--r--js/pico-cam/index.html14
-rw-r--r--js/pico-cam/service-worker.js45
2 files changed, 59 insertions, 0 deletions
diff --git a/js/pico-cam/index.html b/js/pico-cam/index.html
index b524b39..7b8798a 100644
--- a/js/pico-cam/index.html
+++ b/js/pico-cam/index.html
@@ -78,5 +78,19 @@
         <canvas id="ditheredOutput"></canvas>
     </div>
     <script src="video.js"></script>
+    <script>
+        // Check if Service Workers are supported
+        if ('serviceWorker' in navigator) {
+            window.addEventListener('load', function() {
+                navigator.serviceWorker.register('service-worker.js').then(function(registration) {
+                    // Registration was successful
+                    console.log('ServiceWorker registration successful with scope: ', registration.scope);
+                }, function(err) {
+                    // Registration failed
+                    console.log('ServiceWorker registration failed: ', err);
+                });
+            });
+        }
+    </script>
 </body>
 </html>
\ No newline at end of file
diff --git a/js/pico-cam/service-worker.js b/js/pico-cam/service-worker.js
new file mode 100644
index 0000000..75027ed
--- /dev/null
+++ b/js/pico-cam/service-worker.js
@@ -0,0 +1,45 @@
+var CACHE_NAME = 'pico-cam-v1';
+var urlsToCache = [
+  '/',
+  '/index.html',
+  '/video.js'
+];
+
+self.addEventListener('install', function(event) {
+    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);
+            }
+        )
+    );
+});
+
+self.addEventListener('activate', function(event) {
+    var cacheWhitelist = ['pico-cam-v1'];
+    event.waitUntil(
+        caches.keys().then(function(cacheNames) {
+            return Promise.all(
+                cacheNames.map(function(cacheName) {
+                    if (cacheWhitelist.indexOf(cacheName) === -1) {
+                        return caches.delete(cacheName);
+                    }
+                })
+            );
+        })
+    );
+});
\ No newline at end of file