Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ performance/results/*.md
next-env.d.ts


bun.lock
bun.lock

# Project issues file
vrickish.md
25 changes: 0 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 51 additions & 4 deletions public/sw.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// PetChain Service Worker — Cache-first for assets, network-first for API
const CACHE_NAME = "petchain-v1";
const STATIC_ASSETS = ["/", "/favicon.ico"];
// PetChain Service Worker — Offline-first with IndexedDB sync
const CACHE_NAME = "petchain-v2";
const STATIC_ASSETS = ["/", "/favicon.ico", "/offline"];

// ── Install: pre-cache essential assets ──
self.addEventListener("install", (event) => {
Expand All @@ -25,6 +25,27 @@ self.addEventListener("activate", (event) => {
);
});

// ── Background Sync: flush offline queue when connection restores ──
self.addEventListener("sync", (event) => {
if (event.tag === "flush-sync-queue") {
event.waitUntil(flushSyncQueue());
}
});

async function flushSyncQueue() {
const clients = await self.clients.matchAll();
clients.forEach((client) => {
client.postMessage({ type: "BACKGROUND_SYNC_TRIGGERED" });
});
}

// ── Message handling ──
self.addEventListener("message", (event) => {
if (event.data?.type === "SKIP_WAITING") {
self.skipWaiting();
}
});

// ── Fetch: strategy per request type ──
self.addEventListener("fetch", (event) => {
const { request } = event;
Expand All @@ -35,7 +56,13 @@ self.addEventListener("fetch", (event) => {
return;
}

// Network-first for API calls
// Stale-while-revalidate for pet profile and medical data APIs
if (url.pathname.startsWith("/api/v1/pets") || url.pathname.startsWith("/api/v1/medical")) {
event.respondWith(staleWhileRevalidate(request));
return;
}

// Network-first for other API calls
if (url.pathname.startsWith("/api")) {
event.respondWith(networkFirst(request));
return;
Expand Down Expand Up @@ -80,3 +107,23 @@ async function networkFirst(request) {
return cached || new Response("", { status: 408, statusText: "Offline" });
}
}

async function staleWhileRevalidate(request) {
const cached = await caches.match(request);
const fetchPromise = fetch(request).then((response) => {
if (response.ok) {
const cache = caches.open(CACHE_NAME);
cache.then((c) => c.put(request, response.clone()));
}
return response;
}).catch(() => cached);

// Return cached immediately if available, otherwise wait for network
if (cached) {
// Don't block on the network update
fetchPromise.catch(() => {});
return cached;
}

return fetchPromise;
}
Loading
Loading