Files
tortugotchi/sw.js
T
Joel Brock b5499bd618 Initial commit: Tortugotchi sea turtle conservation PWA
Vanilla HTML/CSS/JS Tamagotchi-style game with three species
(green, loggerhead, leatherback), persistent idle progression,
hazard QTEs (sharks, boats, ghost nets, tourists), and offline
PWA support via service worker. Includes README with cited
conservation resources and research.
2026-05-26 16:29:24 -07:00

52 lines
1.2 KiB
JavaScript

const CACHE_NAME = 'tortugotchi-v10';
const ASSETS = [
'./',
'./index.html',
'./style.css',
'./app.js',
'./manifest.json',
'./icon-192.png',
'./icon-512.png'
];
// Install Event
self.addEventListener('install', (e) => {
e.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log('[Service Worker] Caching all app shell assets');
return cache.addAll(ASSETS);
}).then(() => self.skipWaiting())
);
});
// Activate Event
self.addEventListener('activate', (e) => {
e.waitUntil(
caches.keys().then((keys) => {
return Promise.all(
keys.map((key) => {
if (key !== CACHE_NAME) {
console.log('[Service Worker] Removing old cache', key);
return caches.delete(key);
}
})
);
}).then(() => self.clients.claim())
);
});
// Fetch Event
self.addEventListener('fetch', (e) => {
e.respondWith(
caches.match(e.request).then((cachedResponse) => {
// Return cached version or fetch from network
return cachedResponse || fetch(e.request).catch(() => {
// Fallback for document requests if completely offline
if (e.request.destination === 'document') {
return caches.match('./index.html');
}
});
})
);
});