Offline Queue: IndexedDB PhotoQueue, offline routing, SW caching + sync
- PhotoQueue class with IndexedDB store (queued/uploading/done/failed) - uploadSelected() and startBulkProcess() check navigator.onLine and queue offline - Online event auto-drains queue via /api/bulk-process with exponential backoff - Offline banner shows queued count, hides when empty - Service Worker: cache-first static, network-first API, background sync listener - Exponential backoff: 2s → 5s → 15s, then mark failed after 3 retries
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
// EXIF Scanner — Service Worker
|
||||
// Cache-first for static assets, network-first for API.
|
||||
const CACHE = "exif-cache-v1";
|
||||
const STATIC = ["/", "/static/"];
|
||||
|
||||
self.addEventListener("install", (e) => {
|
||||
self.skipWaiting();
|
||||
e.waitUntil(
|
||||
caches.open(CACHE).then((c) => c.addAll(["/", "/static/index.html"]))
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("activate", (e) => {
|
||||
e.waitUntil(clients.claim());
|
||||
});
|
||||
|
||||
self.addEventListener("fetch", (e) => {
|
||||
const url = new URL(e.request.url);
|
||||
// API calls: network-first
|
||||
if (url.pathname.startsWith("/api/")) {
|
||||
e.respondWith(
|
||||
fetch(e.request).catch(() => {
|
||||
return new Response(
|
||||
JSON.stringify({ offline: true, error: "No network connection" }),
|
||||
{ status: 503, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
})
|
||||
);
|
||||
return;
|
||||
}
|
||||
// Static assets: cache-first
|
||||
e.respondWith(
|
||||
caches.match(e.request).then((r) => r || fetch(e.request))
|
||||
);
|
||||
});
|
||||
|
||||
// Background Sync
|
||||
self.addEventListener("sync", (e) => {
|
||||
if (e.tag === "sync-photos") {
|
||||
e.waitUntil(syncPhotos());
|
||||
}
|
||||
});
|
||||
|
||||
async function syncPhotos() {
|
||||
const clients = await self.clients.matchAll();
|
||||
for (const client of clients) {
|
||||
client.postMessage({ type: "SYNC_TRIGGERED" });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user