82 lines
2.2 KiB
JavaScript
82 lines
2.2 KiB
JavaScript
// Aura Cloud Service Worker — High-Performance Offline & Shell Cache v2.0
|
|
const CACHE_NAME = 'aura-cloud-pwa-v2';
|
|
|
|
const STATIC_PRECACHE = [
|
|
'/',
|
|
'/index.html',
|
|
'/manifest.json',
|
|
'/logo.png',
|
|
'/apple-touch-icon.png',
|
|
'/icon-192.png',
|
|
'/icon-512.png',
|
|
'/icon-maskable-192.png',
|
|
'/icon-maskable-512.png',
|
|
'/privacy.html',
|
|
'/terms.html'
|
|
];
|
|
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then((cache) => {
|
|
return cache.addAll(STATIC_PRECACHE).catch((err) => {
|
|
console.warn('[Aura PWA SW] Pre-cache initial warning:', err);
|
|
});
|
|
}).then(() => self.skipWaiting())
|
|
);
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(
|
|
caches.keys().then((keys) => {
|
|
return Promise.all(
|
|
keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k))
|
|
);
|
|
}).then(() => self.clients.claim())
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
const req = event.request;
|
|
const url = new URL(req.url);
|
|
|
|
// 1. Bypass external/cross-origin requests (Auth, Analytics, Fonts, APIs)
|
|
if (url.origin !== self.location.origin) {
|
|
return;
|
|
}
|
|
|
|
// 2. Network-first strategy for navigation / HTML routes (instant updates)
|
|
if (req.mode === 'navigate' || req.headers.get('accept')?.includes('text/html')) {
|
|
event.respondWith(
|
|
fetch(req)
|
|
.then((networkRes) => {
|
|
if (networkRes && networkRes.ok) {
|
|
const clone = networkRes.clone();
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(req, clone));
|
|
}
|
|
return networkRes;
|
|
})
|
|
.catch(() => {
|
|
return caches.match(req).then((cached) => cached || caches.match('/index.html'));
|
|
})
|
|
);
|
|
return;
|
|
}
|
|
|
|
// 3. Stale-while-revalidate for same-origin static assets
|
|
event.respondWith(
|
|
caches.match(req).then((cached) => {
|
|
const fetchPromise = fetch(req)
|
|
.then((networkRes) => {
|
|
if (networkRes && networkRes.ok) {
|
|
const clone = networkRes.clone();
|
|
caches.open(CACHE_NAME).then((cache) => cache.put(req, clone));
|
|
}
|
|
return networkRes;
|
|
})
|
|
.catch(() => null);
|
|
|
|
return cached || fetchPromise;
|
|
})
|
|
);
|
|
});
|