Initialize repo and add session changes (2025-09-08)
This commit is contained in:
42
public/guest-sw.js
Normal file
42
public/guest-sw.js
Normal file
@@ -0,0 +1,42 @@
|
||||
// Minimal service worker for Guest PWA queue sync
|
||||
self.addEventListener('install', (event) => {
|
||||
self.skipWaiting();
|
||||
});
|
||||
|
||||
self.addEventListener('activate', (event) => {
|
||||
event.waitUntil(self.clients.claim());
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', (event) => {
|
||||
const req = event.request;
|
||||
if (req.method !== 'GET' || !req.url.startsWith(self.location.origin)) return;
|
||||
event.respondWith((async () => {
|
||||
const cache = await caches.open('guest-runtime');
|
||||
const cached = await cache.match(req);
|
||||
if (cached) return cached;
|
||||
try {
|
||||
const res = await fetch(req);
|
||||
// Cache static assets and images
|
||||
const ct = res.headers.get('content-type') || '';
|
||||
if (res.ok && (ct.includes('text/css') || ct.includes('javascript') || ct.startsWith('image/'))) {
|
||||
cache.put(req, res.clone());
|
||||
}
|
||||
return res;
|
||||
} catch (e) {
|
||||
return cached || new Response('Offline', { status: 503 });
|
||||
}
|
||||
})());
|
||||
});
|
||||
|
||||
self.addEventListener('sync', (event) => {
|
||||
if (event.tag === 'upload-queue') {
|
||||
event.waitUntil(
|
||||
(async () => {
|
||||
const clients = await self.clients.matchAll({ includeUncontrolled: true, type: 'window' });
|
||||
for (const client of clients) {
|
||||
client.postMessage({ type: 'sync-queue' });
|
||||
}
|
||||
})()
|
||||
);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user