Files
Dramlog-Prod/public/sw.js

66 lines
2.0 KiB
JavaScript

const CACHE_NAME = 'whisky-vault-v2'; // Increment version to force update
const ASSETS_TO_CACHE = [
'/manifest.json',
'/icon-192.png',
'/icon-512.png',
'/favicon.ico',
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(ASSETS_TO_CACHE);
})
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
})
);
self.clients.claim();
});
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
// CRITICAL: Always bypass cache for auth, api, and supabase requests
if (
url.pathname.includes('/auth/') ||
url.pathname.includes('/api/') ||
url.hostname.includes('supabase.co')
) {
return; // Let it fall through to the network
}
// Network first for all other requests, especially navigation
event.respondWith(
fetch(event.request)
.then((response) => {
// Optionally cache successful GET requests for assets
if (
event.request.method === 'GET' &&
response.status === 200 &&
(url.pathname.startsWith('/_next/static/') || ASSETS_TO_CACHE.includes(url.pathname))
) {
const responseClone = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseClone);
});
}
return response;
})
.catch(() => {
return caches.match(event.request);
})
);
});