From 5e90d7722eedc8d678b56b22c4ee6d42ae14d021 Mon Sep 17 00:00:00 2001 From: "Brett V. Forsgren" Date: Thu, 15 Feb 2024 17:32:45 -0700 Subject: [PATCH] make it installable as a pwa --- src/index.ts | 8 +++++ src/resources/icon.svg | 4 +++ src/resources/index.html | 2 ++ src/resources/manifest.json.webmanifest | 12 +++++++ src/resources/service-worker.js | 45 +++++++++++++++++++++++++ 5 files changed, 71 insertions(+) create mode 100644 src/resources/icon.svg create mode 100644 src/resources/manifest.json.webmanifest create mode 100644 src/resources/service-worker.js diff --git a/src/index.ts b/src/index.ts index 322dcf2..aceb4b8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,8 @@ import { Expression } from "./expression.js"; function main() { + installServiceWorker(); + const results = document.getElementById('results')!; const input = document.getElementById('input')!; const evalButton = document.getElementById('eval')!; @@ -25,4 +27,10 @@ function main() { }); } +function installServiceWorker() { + if ('serviceWorker' in navigator) { + navigator.serviceWorker.register('service-worker.js'); + } +} + window.addEventListener('DOMContentLoaded', main); diff --git a/src/resources/icon.svg b/src/resources/icon.svg new file mode 100644 index 0000000..3e9a97c --- /dev/null +++ b/src/resources/icon.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/src/resources/index.html b/src/resources/index.html index cf5df10..446131c 100644 --- a/src/resources/index.html +++ b/src/resources/index.html @@ -1,7 +1,9 @@ + + diff --git a/src/resources/manifest.json.webmanifest b/src/resources/manifest.json.webmanifest new file mode 100644 index 0000000..a531ea3 --- /dev/null +++ b/src/resources/manifest.json.webmanifest @@ -0,0 +1,12 @@ +{ + "name": "IxMilia Calculator", + "icons": [ + { + "src": "icon.svg", + "type": "image/svg+xml", + "sizes": "128x128" + } + ], + "start_url": "/", + "display": "standalone" +} \ No newline at end of file diff --git a/src/resources/service-worker.js b/src/resources/service-worker.js new file mode 100644 index 0000000..0cd5af9 --- /dev/null +++ b/src/resources/service-worker.js @@ -0,0 +1,45 @@ +const cacheName = 'ixmilia.calc.cache'; +const filesToCache = [ + '/', + 'index.html', + 'app.js', + 'icon.svg', +]; + +self.addEventListener('install', (e) => { + e.waitUntil(() => { + caches.open(cacheName) + .then(cache => cache.addAll(filesToCache)) + .then(self.skipWaiting()); + }); +}); + +self.addEventListener('activate', (e) => { + e.waitUntil(() => { + caches.keys().then(keys => { + return keys.filter(key => key !== cacheName); + }).then(cachesToDelete => { + return Promise.all(cachesToDelete.map(cacheToDelete => { + return caches.delete(cacheToDelete); + })); + }).then(() => self.clients.claim()); + }); +}); + +self.addEventListener('fetch', (e) => { + e.respondWith(() => { + const response = caches.match(e.request); + if (response) { + return response; + } + + console.log(`Cache miss: ${e.request.url}`); + return fetch(e.request).then(response => { + cache.put(e.request, response.clone()) + .then(() => { + console.log(`Cached: ${e.request.url}`); + return response; + }); + }); + }); +});