From 087292f65d118474c9fc88710656b2e65d7616a5 Mon Sep 17 00:00:00 2001 From: robin Date: Thu, 18 Dec 2025 21:27:00 +0100 Subject: [PATCH] feat: improve PWA auth robustness and session management --- public/sw.js | 20 +++++++++++++------- src/app/page.tsx | 21 +++++++++++++-------- src/components/PWARegistration.tsx | 15 +++++++++++++++ 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/public/sw.js b/public/sw.js index 52d3c5f..d520c73 100644 --- a/public/sw.js +++ b/public/sw.js @@ -33,12 +33,15 @@ 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 + const isAuthRequest = url.pathname.includes('/auth/') || + url.pathname.includes('/v1/auth/') || + url.pathname.includes('/v1/token'); + const isApiRequest = url.pathname.includes('/api/'); + const isSupabaseRequest = url.hostname.includes('supabase.co'); + + if (isAuthRequest || isApiRequest || isSupabaseRequest) { + // console.log('[SW] Bypassing cache for:', url.pathname); + return; } // Network first for all other requests, especially navigation @@ -58,7 +61,10 @@ self.addEventListener('fetch', (event) => { } return response; }) - .catch(() => { + .catch((err) => { + if (event.request.mode === 'navigate') { + console.log('[SW] Navigation failed, attempting cache fallback for:', url.pathname); + } return caches.match(event.request); }) ); diff --git a/src/app/page.tsx b/src/app/page.tsx index 77c2b6d..e4026f4 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -24,23 +24,28 @@ export default function Home() { // Check session const checkUser = async () => { try { + // Proactively get session - this will trigger a refresh if needed const { data: { session }, error } = await supabase.auth.getSession(); - console.log('[Auth] Initial session check:', { - hasSession: !!session, - userId: session?.user?.id, - email: session?.user?.email, - error: error?.message - }); + + if (session) { + console.log('[Auth] Valid session found:', { + userId: session.user.id, + expiry: new Date(session.expires_at! * 1000).toLocaleString() + }); + } else { + console.log('[Auth] No active session found.'); + } if (error) { - console.error('[Auth] Session retrieval error:', error); + console.error('[Auth] Session check error:', error); } + setUser(session?.user ?? null); if (session?.user) { fetchCollection(); } } catch (err) { - console.error('[Auth] Fatal error checking user:', err); + console.error('[Auth] Unexpected error during session check:', err); setUser(null); } finally { setIsLoading(false); diff --git a/src/components/PWARegistration.tsx b/src/components/PWARegistration.tsx index a82df5c..7d70423 100644 --- a/src/components/PWARegistration.tsx +++ b/src/components/PWARegistration.tsx @@ -10,6 +10,21 @@ export default function PWARegistration() { .register('/sw.js') .then((registration) => { console.log('SW registered: ', registration); + + // Check for updates + registration.onupdatefound = () => { + const installingWorker = registration.installing; + if (installingWorker == null) return; + installingWorker.onstatechange = () => { + if (installingWorker.state === 'installed') { + if (navigator.serviceWorker.controller) { + console.log('[SW] New content is available; please refresh.'); + } else { + console.log('[SW] Content is cached for offline use.'); + } + } + }; + }; }) .catch((registrationError) => { console.log('SW registration failed: ', registrationError);