// src/sw.ts
import { precacheAndRoute } from 'workbox-precaching';
import { registerRoute } from 'workbox-routing';
import { NetworkFirst, CacheFirst } from 'workbox-strategies';
// Precache static assets
precacheAndRoute(self.__WB_MANIFEST);
// API requests: Network first, fall back to cache
registerRoute(
({ url }) => url.pathname.startsWith('/api'),
new NetworkFirst({
cacheName: 'api-cache',
networkTimeoutSeconds: 10,
})
);
// Images: Cache first
registerRoute(
({ request }) => request.destination === 'image',
new CacheFirst({
cacheName: 'image-cache',
})
);
// Push notifications
self.addEventListener('push', (event) => {
const data = event.data?.json();
event.waitUntil(
self.registration.showNotification(data.title, {
body: data.body,
icon: '/icons/icon-192.png',
data: data.data,
})
);
});