feat(pwa): rewrite service worker (shell cache + data cache + offline fallback)
This commit is contained in:
+82
-104
@@ -1,130 +1,108 @@
|
|||||||
// Service Worker for PWA - Caching and offline support
|
// public/sw.js
|
||||||
|
// Route Commerce Service Worker
|
||||||
|
// Two-cache strategy: shell (cache-first) + data (stale-while-revalidate)
|
||||||
|
|
||||||
const CACHE_NAME = "route-commerce-v1";
|
const SHELL_CACHE = "rc-shell-v3";
|
||||||
const OFFLINE_URL = "/offline";
|
const DATA_CACHE = "rc-data-v3";
|
||||||
|
const OFFLINE_URL = "/offline.html";
|
||||||
|
|
||||||
const STATIC_ASSETS = [
|
const SHELL_ASSETS = [
|
||||||
"/",
|
"/",
|
||||||
"/manifest.json",
|
"/manifest.json",
|
||||||
"/favicon.svg",
|
"/favicon.svg",
|
||||||
"/og-default.jpg",
|
"/og-default.svg",
|
||||||
|
"/offline.html",
|
||||||
|
"/icons/icon-192x192.png",
|
||||||
|
"/icons/icon-512x512.png",
|
||||||
];
|
];
|
||||||
|
|
||||||
// Install event - cache static assets
|
|
||||||
self.addEventListener("install", (event) => {
|
self.addEventListener("install", (event) => {
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.open(CACHE_NAME).then((cache) => {
|
caches.open(SHELL_CACHE).then((cache) => cache.addAll(SHELL_ASSETS))
|
||||||
return cache.addAll(STATIC_ASSETS);
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
self.skipWaiting();
|
self.skipWaiting();
|
||||||
});
|
});
|
||||||
|
|
||||||
// Activate event - clean old caches
|
|
||||||
self.addEventListener("activate", (event) => {
|
self.addEventListener("activate", (event) => {
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.keys().then((cacheNames) => {
|
Promise.all([
|
||||||
return Promise.all(
|
caches.keys().then((cacheNames) =>
|
||||||
cacheNames
|
Promise.all(
|
||||||
.filter((name) => name !== CACHE_NAME)
|
cacheNames
|
||||||
.map((name) => caches.delete(name))
|
.filter((name) => name !== SHELL_CACHE && name !== DATA_CACHE)
|
||||||
);
|
.map((name) => caches.delete(name))
|
||||||
})
|
)
|
||||||
|
),
|
||||||
|
self.clients.claim(),
|
||||||
|
])
|
||||||
);
|
);
|
||||||
self.clients.claim();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Fetch event - network first, fallback to cache
|
|
||||||
self.addEventListener("fetch", (event) => {
|
self.addEventListener("fetch", (event) => {
|
||||||
// Skip non-GET requests
|
const req = event.request;
|
||||||
if (event.request.method !== "GET") return;
|
if (req.method !== "GET") return;
|
||||||
|
const url = new URL(req.url);
|
||||||
|
if (url.origin !== self.location.origin) return;
|
||||||
|
|
||||||
// Skip API requests
|
// Navigations → network-first, fall back to cache, fall back to offline page
|
||||||
if (event.request.url.includes("/api/")) return;
|
if (req.mode === "navigate") {
|
||||||
|
event.respondWith(
|
||||||
|
fetch(req)
|
||||||
|
.then((res) => {
|
||||||
|
const clone = res.clone();
|
||||||
|
caches.open(SHELL_CACHE).then((cache) => cache.put(req, clone));
|
||||||
|
return res;
|
||||||
|
})
|
||||||
|
.catch(() =>
|
||||||
|
caches.match(req).then((cached) => cached || caches.match(OFFLINE_URL))
|
||||||
|
)
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Skip cross-origin requests
|
// Static assets → cache-first
|
||||||
if (!event.request.url.startsWith(self.location.origin)) return;
|
if (
|
||||||
|
url.pathname.startsWith("/_next/static/") ||
|
||||||
event.respondWith(
|
url.pathname.startsWith("/icons/") ||
|
||||||
fetch(event.request)
|
url.pathname.startsWith("/screenshots/") ||
|
||||||
.then((response) => {
|
url.pathname.endsWith(".svg") ||
|
||||||
// Clone response for caching
|
url.pathname.endsWith(".woff2")
|
||||||
const responseClone = response.clone();
|
) {
|
||||||
|
event.respondWith(
|
||||||
// Cache successful responses
|
caches.match(req).then((cached) => {
|
||||||
if (response.status === 200) {
|
if (cached) return cached;
|
||||||
caches.open(CACHE_NAME).then((cache) => {
|
return fetch(req).then((res) => {
|
||||||
cache.put(event.request, responseClone);
|
const clone = res.clone();
|
||||||
});
|
if (res.status === 200) {
|
||||||
}
|
caches.open(SHELL_CACHE).then((cache) => cache.put(req, clone));
|
||||||
|
|
||||||
return response;
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
// Return cached response or offline page
|
|
||||||
return caches.match(event.request).then((cachedResponse) => {
|
|
||||||
if (cachedResponse) {
|
|
||||||
return cachedResponse;
|
|
||||||
}
|
}
|
||||||
// Return offline page for navigation requests
|
return res;
|
||||||
if (event.request.mode === "navigate") {
|
|
||||||
return caches.match(OFFLINE_URL);
|
|
||||||
}
|
|
||||||
return new Response("Network error", { status: 408 });
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
return;
|
||||||
|
|
||||||
// Push notification handling
|
|
||||||
self.addEventListener("push", (event) => {
|
|
||||||
if (!event.data) return;
|
|
||||||
|
|
||||||
const data = event.data.json();
|
|
||||||
|
|
||||||
const options = {
|
|
||||||
body: data.body,
|
|
||||||
icon: "/icons/icon-192x192.png",
|
|
||||||
badge: "/icons/badge-72x72.png",
|
|
||||||
vibrate: [100, 50, 100],
|
|
||||||
data: {
|
|
||||||
url: data.url || "/",
|
|
||||||
},
|
|
||||||
actions: data.actions || [],
|
|
||||||
};
|
|
||||||
|
|
||||||
event.waitUntil(self.registration.showNotification(data.title, options));
|
|
||||||
});
|
|
||||||
|
|
||||||
// Notification click handling
|
|
||||||
self.addEventListener("notificationclick", (event) => {
|
|
||||||
event.notification.close();
|
|
||||||
|
|
||||||
const url = event.notification.data?.url || "/";
|
|
||||||
|
|
||||||
event.waitUntil(
|
|
||||||
clients.matchAll({ type: "window", includeUncontrolled: true }).then((clientList) => {
|
|
||||||
// Focus existing window or open new one
|
|
||||||
for (const client of clientList) {
|
|
||||||
if (client.url === url && "focus" in client) {
|
|
||||||
return client.focus();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (clients.openWindow) {
|
|
||||||
return clients.openWindow(url);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Background sync for offline actions
|
|
||||||
self.addEventListener("sync", (event) => {
|
|
||||||
if (event.tag === "sync-orders") {
|
|
||||||
event.waitUntil(syncOrders());
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
async function syncOrders() {
|
// API GETs (read-only data) → stale-while-revalidate
|
||||||
// Implement order sync logic
|
if (url.pathname.startsWith("/api/")) {
|
||||||
console.log("Syncing orders in background...");
|
event.respondWith(
|
||||||
}
|
caches.open(DATA_CACHE).then((cache) =>
|
||||||
|
cache.match(req).then((cached) => {
|
||||||
|
const network = fetch(req)
|
||||||
|
.then((res) => {
|
||||||
|
if (res.status === 200) cache.put(req, res.clone());
|
||||||
|
return res;
|
||||||
|
})
|
||||||
|
.catch(() => cached);
|
||||||
|
return cached || network;
|
||||||
|
})
|
||||||
|
)
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default: try network, fall back to cache
|
||||||
|
event.respondWith(
|
||||||
|
fetch(req).catch(() => caches.match(req))
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user