Repository navigation
Expand file tree
/
Copy pathservice-worker.js
More file actions
111 lines (99 loc) · 3.73 KB
/
Copy pathservice-worker.js
File metadata and controls
111 lines (99 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// QuickBill Service Worker - Optimized Version
// Version: 2.1.0
// Supports both root (/) and subdirectory (/QuickBill/) deployment
const CACHE_NAME = 'quickbill-pwa-v2';
// Auto-detect base path from service worker location
const getBasePath = () => {
const swPath = self.location.pathname;
const basePath = swPath.substring(0, swPath.lastIndexOf('/') + 1);
return basePath || '/';
};
const BASE_PATH = getBasePath();
// Essential local files to cache
const localAssets = [
`${BASE_PATH}`,
`${BASE_PATH}index.html`,
`${BASE_PATH}manifest.json`,
`${BASE_PATH}icons/icon-192x192.png`,
`${BASE_PATH}icons/icon-512x512.png`
];
// CDN resources - cached at runtime to avoid CORS issues
const cdnResources = [
'https://cdn.tailwindcss.com',
'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap',
'https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js'
];
// Install event - cache local assets only
self.addEventListener('install', (event) => {
console.log('Service Worker: Installing with base path:', BASE_PATH);
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('Service Worker: Caching local assets');
return cache.addAll(localAssets);
})
.catch(error => {
console.error('Service Worker: Install failed:', error);
// Don't fail install if some assets missing
return Promise.resolve();
})
);
self.skipWaiting();
});
// Fetch event - network first, fallback to cache
self.addEventListener('fetch', (event) => {
const requestUrl = event.request.url;
// Skip non-GET requests
if (event.request.method !== 'GET') {
return;
}
// Skip Firebase/external auth requests
if (requestUrl.includes('firebaseapp.com') ||
requestUrl.includes('googleapis.com/identitytoolkit') ||
requestUrl.includes('securetoken.googleapis.com')) {
return;
}
event.respondWith(
fetch(event.request)
.then((response) => {
// Valid response - clone and cache it
if (response && response.status === 200) {
const responseToCache = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseToCache);
});
}
return response;
})
.catch(() => {
// Network failed - try cache
return caches.match(event.request)
.then((cachedResponse) => {
if (cachedResponse) {
return cachedResponse;
}
// No cache - return offline page or error
console.log('Service Worker: Resource not in cache:', requestUrl);
throw new Error('Offline and resource not cached');
});
})
);
});
// Activate event - cleanup old caches
self.addEventListener('activate', (event) => {
console.log('Service Worker: Activating');
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
console.log('Service Worker: Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
})
);
return self.clients.claim();
});