Add PWA support with service worker and icons

This commit is contained in:
chris 2025-11-22 20:30:14 -05:00
parent 4f47f057c4
commit ba41a799b3
5 changed files with 83 additions and 0 deletions

BIN
icons/icon-192.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

BIN
icons/icon-512.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 266 KiB

View File

@ -4,6 +4,9 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Hogwarts Arithmancy v15</title> <title>Hogwarts Arithmancy v15</title>
<meta name="theme-color" content="#740001">
<link rel="manifest" href="manifest.webmanifest">
<link rel="apple-touch-icon" href="icons/icon-192.png">
<link href="https://fonts.googleapis.com/css2?family=Cinzel:wght@400;700&family=MedievalSharp&family=Dancing+Script:wght@700&display=swap" rel="stylesheet"> <link href="https://fonts.googleapis.com/css2?family=Cinzel:wght@400;700&family=MedievalSharp&family=Dancing+Script:wght@700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css"> <link rel="stylesheet" href="style.css">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
@ -117,5 +120,14 @@
</div> </div>
<script src="app.js"></script> <script src="app.js"></script>
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('./service-worker.js').catch(err => {
console.warn('Service worker registration failed', err);
});
});
}
</script>
</body> </body>
</html> </html>

20
manifest.webmanifest Normal file
View File

@ -0,0 +1,20 @@
{
"name": "Hogwarts Arithmancy",
"short_name": "Arithmancy",
"start_url": "./",
"display": "standalone",
"background_color": "#0c0b0b",
"theme_color": "#740001",
"icons": [
{
"src": "icons/icon-192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "icons/icon-512.png",
"sizes": "512x512",
"type": "image/png"
}
]
}

51
service-worker.js Normal file
View File

@ -0,0 +1,51 @@
const CACHE_NAME = 'arithmancy-pwa-v1';
const ASSETS = [
'./',
'./index.html',
'./style.css',
'./app.js',
'./manifest.webmanifest',
'./icons/icon-192.png',
'./icons/icon-512.png'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => cache.addAll(ASSETS))
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key)))
)
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
if (event.request.method !== 'GET') return;
event.respondWith(
caches.match(event.request).then((cached) => {
if (cached) return cached;
return fetch(event.request)
.then((response) => {
const shouldCache =
response && response.status === 200 && response.type === 'basic' && event.request.url.startsWith(self.location.origin);
if (shouldCache) {
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
}
return response;
})
.catch(() => {
if (event.request.mode === 'navigate') {
return caches.match('./');
}
});
})
);
});