59 lines
1.5 KiB
JavaScript
59 lines
1.5 KiB
JavaScript
// Pacific Trans GO - Service Worker PWA
|
|
const CACHE_NAME = 'pacifictrans-go-v1';
|
|
const STATIC_ASSETS = [
|
|
'/',
|
|
'/index.html',
|
|
'/css/style.css',
|
|
'/js/app.js',
|
|
'/manifest.json',
|
|
'/icons/icon-192.png',
|
|
'/icons/icon-512.png',
|
|
'/icons/apple-touch-icon.png',
|
|
'/images/hero-banner.jpg'
|
|
];
|
|
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => {
|
|
console.log('⚡ [PWA] Pre-caching static assets');
|
|
return cache.addAll(STATIC_ASSETS).catch((err) => {
|
|
console.warn('⚠️ [PWA Pre-cache error]:', err);
|
|
});
|
|
})
|
|
);
|
|
self.skipWaiting();
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((keys) => {
|
|
return Promise.all(
|
|
keys.filter((key) => key !== CACHE_NAME).map((key) => caches.delete(key))
|
|
);
|
|
})
|
|
);
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
// Don't intercept API or non-GET requests
|
|
if (event.request.method !== 'GET' || event.request.url.includes('/api/')) {
|
|
return;
|
|
}
|
|
|
|
event.respondWith(
|
|
caches.match(event.request).then((cachedResponse) => {
|
|
if (cachedResponse) {
|
|
// Fetch background update for cache
|
|
fetch(event.request).then((networkResponse) => {
|
|
if (networkResponse && networkResponse.status === 200) {
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, networkResponse));
|
|
}
|
|
}).catch(() => {});
|
|
return cachedResponse;
|
|
}
|
|
return fetch(event.request);
|
|
})
|
|
);
|
|
});
|