34 lines
1.0 KiB
JavaScript
34 lines
1.0 KiB
JavaScript
// Simple service worker for PWA
|
|
const CACHE_NAME = 'zhuiguang-v1';
|
|
|
|
self.addEventListener('install', (event) => {
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(clients.claim());
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
// Network-first strategy for API routes
|
|
// Cache-first for static assets
|
|
const url = new URL(event.request.url);
|
|
|
|
if (url.pathname.startsWith('/_next/') || url.pathname.includes('/fonts/') || url.pathname.includes('/logos/')) {
|
|
// Cache static assets
|
|
event.respondWith(
|
|
caches.match(event.request).then((cached) => {
|
|
const fetchPromise = fetch(event.request).then((response) => {
|
|
if (response.ok) {
|
|
const clone = response.clone();
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
|
|
}
|
|
return response;
|
|
});
|
|
return cached || fetchPromise;
|
|
})
|
|
);
|
|
}
|
|
// All other requests: network-only (don't cache dynamic content)
|
|
});
|