feat: Update PWA service worker for authentication
- Add authentication pages to static cache (/auth/login, /auth/register, /auth/error) - Implement special handling for authentication API requests (network-only with offline error) - Add authentication page caching strategy (cache-first with network fallback) - Prevent caching of authentication errors (401, 403) - Enhance offline page with online detection and auto-redirect - Add authentication notice to offline page - Update cache version to v2 for proper invalidation - Create PWA_AUTH_GUIDE.md documentation Task: 19. Update PWA service worker for authentication Requirements: 4.4
This commit is contained in:
78
public/sw.js
78
public/sw.js
@@ -1,13 +1,16 @@
|
||||
// Service Worker for Ultimate Lists App
|
||||
// Implements caching strategies for offline functionality
|
||||
|
||||
const CACHE_NAME = 'ultimate-lists-v1';
|
||||
const RUNTIME_CACHE = 'ultimate-lists-runtime-v1';
|
||||
const CACHE_NAME = 'ultimate-lists-v2';
|
||||
const RUNTIME_CACHE = 'ultimate-lists-runtime-v2';
|
||||
|
||||
// Static assets to cache on install
|
||||
const STATIC_ASSETS = [
|
||||
'/',
|
||||
'/offline',
|
||||
'/auth/login',
|
||||
'/auth/register',
|
||||
'/auth/error',
|
||||
'/manifest.json',
|
||||
'/icon-192.svg',
|
||||
'/icon-512.svg',
|
||||
@@ -58,6 +61,12 @@ self.addEventListener('fetch', (event) => {
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle authentication-related requests
|
||||
if (isAuthRequest(url)) {
|
||||
event.respondWith(handleAuthRequest(request));
|
||||
return;
|
||||
}
|
||||
|
||||
// Cache-first strategy for static assets
|
||||
if (isStaticAsset(url)) {
|
||||
event.respondWith(cacheFirst(request));
|
||||
@@ -90,6 +99,58 @@ function isPageRequest(url) {
|
||||
return url.origin === self.location.origin && !url.pathname.includes('.');
|
||||
}
|
||||
|
||||
// Helper: Check if request is authentication-related
|
||||
function isAuthRequest(url) {
|
||||
return url.pathname.startsWith('/auth/') ||
|
||||
url.pathname.startsWith('/api/auth/');
|
||||
}
|
||||
|
||||
// Strategy: Handle authentication requests
|
||||
async function handleAuthRequest(request) {
|
||||
const url = new URL(request.url);
|
||||
|
||||
// For authentication API calls, always try network first
|
||||
if (url.pathname.startsWith('/api/auth/')) {
|
||||
try {
|
||||
const response = await fetch(request);
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('Authentication API request failed (offline):', error);
|
||||
// Return a JSON error response for API calls
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
error: 'Authentication requires an internet connection. Please check your connection and try again.'
|
||||
}),
|
||||
{
|
||||
status: 503,
|
||||
statusText: 'Service Unavailable',
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// For authentication pages, use cache-first with network fallback
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
const cached = await cache.match(request);
|
||||
|
||||
try {
|
||||
const response = await fetch(request);
|
||||
if (response.ok) {
|
||||
cache.put(request, response.clone());
|
||||
}
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.error('Authentication page request failed:', error);
|
||||
// Return cached version if available
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
// Otherwise redirect to offline page
|
||||
return caches.match('/offline');
|
||||
}
|
||||
}
|
||||
|
||||
// Strategy: Cache-first (for static assets)
|
||||
async function cacheFirst(request) {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
@@ -117,6 +178,12 @@ async function staleWhileRevalidate(request) {
|
||||
const cached = await cache.match(request);
|
||||
|
||||
const fetchPromise = fetch(request).then((response) => {
|
||||
// Handle authentication errors (401, 403)
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
// Don't cache authentication errors
|
||||
return response;
|
||||
}
|
||||
|
||||
if (response.ok) {
|
||||
cache.put(request, response.clone());
|
||||
}
|
||||
@@ -141,6 +208,13 @@ async function staleWhileRevalidate(request) {
|
||||
async function networkFirst(request) {
|
||||
try {
|
||||
const response = await fetch(request);
|
||||
|
||||
// Handle authentication errors (401, 403)
|
||||
if (response.status === 401 || response.status === 403) {
|
||||
// Don't cache authentication errors
|
||||
return response;
|
||||
}
|
||||
|
||||
if (response.ok) {
|
||||
const cache = await caches.open(RUNTIME_CACHE);
|
||||
cache.put(request, response.clone());
|
||||
|
||||
Reference in New Issue
Block a user