Skip to content

Commit

Permalink
make it installable as a pwa
Browse files Browse the repository at this point in the history
  • Loading branch information
brettfo committed Feb 16, 2024
1 parent f059531 commit 76297fc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Expression } from "./expression.js";

function main() {
installServiceWorker();

const results = <HTMLTextAreaElement>document.getElementById('results')!;
const input = <HTMLInputElement>document.getElementById('input')!;
const evalButton = <HTMLButtonElement>document.getElementById('eval')!;
Expand All @@ -25,4 +27,10 @@ function main() {
});
}

function installServiceWorker() {
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('service-worker.js');
}
}

window.addEventListener('DOMContentLoaded', main);
4 changes: 4 additions & 0 deletions src/resources/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/resources/index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<html>

<head>
<link rel="manifest" href="manifest.json.webmanifest" />
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript" src="service-worker.js"></script>
</head>

<body>
Expand Down
12 changes: 12 additions & 0 deletions src/resources/manifest.json.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "IxMilia Calculator",
"icons": [
{
"src": "icon.svg",
"type": "image/svg+xml",
"sizes": "128x128"
}
],
"start_url": "/",
"display": "standalone"
}
32 changes: 32 additions & 0 deletions src/resources/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const cacheName = 'ixmilia.calc.cache';
const filesToCache = [
'/',
'/app.js',
'/icon.svg',
'/index.html',
];

self.addEventListener('install', (e) => {
e.waitUntil(async () => {
const cache = await caches.open(cacheName);
await cache.addAll(filesToCache);
});
});

self.addEventListener('fetch', (e) => {
e.respondWith(async () => {
const response = await caches.match(e.request);
return response || fetch(e.request);
});
});

self.addEventListener('activate', (e) => {
e.waitUntil(async () => {
const keys = await caches.keys();
await Promise.all(keys.map((key) => {
if (key !== cacheName) {
return caches.delete(key);
}
}));
});
});

0 comments on commit 76297fc

Please sign in to comment.