Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
803 lines (701 loc) · 22.1 KB
/
Copy pathsw.js
File metadata and controls
803 lines (701 loc) · 22.1 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
/**
* Service Worker - Offline functionality and caching for image converter app
* Implements intelligent caching strategies and offline fallbacks
*/
const CACHE_VERSION = 'v1.2.0';
const STATIC_CACHE = `static-${CACHE_VERSION}`;
const DYNAMIC_CACHE = `dynamic-${CACHE_VERSION}`;
const API_CACHE = `api-${CACHE_VERSION}`;
// Cache duration in milliseconds
const CACHE_DURATIONS = {
static: 7 * 24 * 60 * 60 * 1000, // 7 days
dynamic: 24 * 60 * 60 * 1000, // 1 day
api: 5 * 60 * 1000, // 5 minutes
images: 30 * 24 * 60 * 60 * 1000 // 30 days
};
// Static assets to cache immediately
const STATIC_ASSETS = [
'/',
'/index.html',
'/dashboard.html',
'/auth.html',
'/pricing.html',
'/js/supabase-client.js',
'/js/auth-manager.js',
'/js/cache-manager.js',
'/js/lazy-loader.js',
'/js/config.js',
'/js/error-handler.js',
'/styles/styles.css',
'/styles/shadcn-tokens.css',
'/styles/shadcn-bridge.css',
// Add critical tool pages
'/tools/image-converter/index.html',
'/tools/pdf-merger/index.html',
'/tools/background-remover/index.html'
];
// API endpoints to cache
const API_ENDPOINTS = [
'/api/user/profile',
'/api/user/usage',
'/api/user/files'
];
// Network-first resources (always try network first)
const NETWORK_FIRST = [
'/api/auth/',
'/api/stripe/',
'/api/upload',
'/api/convert'
];
// Cache-first resources (serve from cache if available)
const CACHE_FIRST = [
'/js/',
'/css/',
'/styles/',
'/images/',
'/icons/'
];
/**
* Install event - cache static assets
*/
self.addEventListener('install', (event) => {
console.log('Service Worker installing...');
event.waitUntil(
Promise.all([
// Cache static assets
caches.open(STATIC_CACHE).then((cache) => {
console.log('Caching static assets...');
return cache.addAll(STATIC_ASSETS.map(url => new Request(url, { cache: 'reload' })));
}),
// Initialize dynamic cache
caches.open(DYNAMIC_CACHE),
// Initialize API cache
caches.open(API_CACHE)
]).then(() => {
console.log('Service Worker installed successfully');
// Skip waiting to activate immediately
return self.skipWaiting();
}).catch((error) => {
console.error('Service Worker installation failed:', error);
})
);
});
/**
* Activate event - clean up old caches
*/
self.addEventListener('activate', (event) => {
console.log('Service Worker activating...');
event.waitUntil(
Promise.all([
// Clean up old caches
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== STATIC_CACHE &&
cacheName !== DYNAMIC_CACHE &&
cacheName !== API_CACHE) {
console.log('Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
}),
// Take control of all clients
self.clients.claim()
]).then(() => {
console.log('Service Worker activated successfully');
})
);
});
/**
* Fetch event - handle all network requests
*/
self.addEventListener('fetch', (event) => {
const { request } = event;
const url = new URL(request.url);
// Skip non-GET requests and chrome-extension requests
if (request.method !== 'GET' || url.protocol === 'chrome-extension:') {
return;
}
// Handle different types of requests
if (isAPIRequest(url)) {
event.respondWith(handleAPIRequest(request));
} else if (isStaticAsset(url)) {
event.respondWith(handleStaticAsset(request));
} else if (isImageRequest(url)) {
event.respondWith(handleImageRequest(request));
} else if (isNetworkFirst(url)) {
event.respondWith(handleNetworkFirst(request));
} else if (isCacheFirst(url)) {
event.respondWith(handleCacheFirst(request));
} else {
event.respondWith(handleDynamicRequest(request));
}
});
/**
* Handle API requests with network-first strategy
*/
async function handleAPIRequest(request) {
const url = new URL(request.url);
try {
// Always try network first for API requests
const networkResponse = await fetch(request);
if (networkResponse.ok) {
// Cache successful API responses
const cache = await caches.open(API_CACHE);
const responseClone = networkResponse.clone();
// Add timestamp for cache expiration
const response = new Response(responseClone.body, {
status: responseClone.status,
statusText: responseClone.statusText,
headers: {
...Object.fromEntries(responseClone.headers.entries()),
'sw-cached-at': Date.now().toString()
}
});
cache.put(request, response);
return networkResponse;
}
throw new Error(`API request failed: ${networkResponse.status}`);
} catch (error) {
console.log('API network request failed, trying cache:', error.message);
// Try cache as fallback
const cachedResponse = await getCachedResponse(API_CACHE, request);
if (cachedResponse && !isCacheExpired(cachedResponse, CACHE_DURATIONS.api)) {
console.log('Serving API response from cache');
return cachedResponse;
}
// Return offline fallback for API requests
return createOfflineAPIResponse(url);
}
}
/**
* Handle static assets with cache-first strategy
*/
async function handleStaticAsset(request) {
try {
// Try cache first
const cachedResponse = await getCachedResponse(STATIC_CACHE, request);
if (cachedResponse) {
return cachedResponse;
}
// Fallback to network
const networkResponse = await fetch(request);
if (networkResponse.ok) {
const cache = await caches.open(STATIC_CACHE);
cache.put(request, networkResponse.clone());
}
return networkResponse;
} catch (error) {
console.log('Static asset request failed:', error.message);
// Try dynamic cache as last resort
const dynamicCached = await getCachedResponse(DYNAMIC_CACHE, request);
if (dynamicCached) {
return dynamicCached;
}
// Return offline fallback
return createOfflineFallback(request);
}
}
/**
* Handle image requests with cache-first strategy and long TTL
*/
async function handleImageRequest(request) {
try {
// Try cache first
const cachedResponse = await getCachedResponse(DYNAMIC_CACHE, request);
if (cachedResponse && !isCacheExpired(cachedResponse, CACHE_DURATIONS.images)) {
return cachedResponse;
}
// Fetch from network
const networkResponse = await fetch(request);
if (networkResponse.ok) {
const cache = await caches.open(DYNAMIC_CACHE);
const responseWithTimestamp = new Response(networkResponse.body, {
status: networkResponse.status,
statusText: networkResponse.statusText,
headers: {
...Object.fromEntries(networkResponse.headers.entries()),
'sw-cached-at': Date.now().toString()
}
});
cache.put(request, responseWithTimestamp.clone());
return networkResponse;
}
throw new Error(`Image request failed: ${networkResponse.status}`);
} catch (error) {
console.log('Image request failed:', error.message);
// Return cached version even if expired
const cachedResponse = await getCachedResponse(DYNAMIC_CACHE, request);
if (cachedResponse) {
return cachedResponse;
}
// Return placeholder image
return createImagePlaceholder();
}
}
/**
* Handle network-first requests
*/
async function handleNetworkFirst(request) {
try {
const networkResponse = await fetch(request);
if (networkResponse.ok) {
// Cache successful responses
const cache = await caches.open(DYNAMIC_CACHE);
cache.put(request, networkResponse.clone());
}
return networkResponse;
} catch (error) {
console.log('Network-first request failed, trying cache:', error.message);
const cachedResponse = await getCachedResponse(DYNAMIC_CACHE, request);
if (cachedResponse) {
return cachedResponse;
}
return createOfflineFallback(request);
}
}
/**
* Handle cache-first requests
*/
async function handleCacheFirst(request) {
try {
// Try cache first
const cachedResponse = await getCachedResponse(DYNAMIC_CACHE, request);
if (cachedResponse && !isCacheExpired(cachedResponse, CACHE_DURATIONS.dynamic)) {
return cachedResponse;
}
// Fetch from network
const networkResponse = await fetch(request);
if (networkResponse.ok) {
const cache = await caches.open(DYNAMIC_CACHE);
const responseWithTimestamp = new Response(networkResponse.body, {
status: networkResponse.status,
statusText: networkResponse.statusText,
headers: {
...Object.fromEntries(networkResponse.headers.entries()),
'sw-cached-at': Date.now().toString()
}
});
cache.put(request, responseWithTimestamp.clone());
return networkResponse;
}
throw new Error(`Cache-first request failed: ${networkResponse.status}`);
} catch (error) {
console.log('Cache-first request failed:', error.message);
// Return cached version even if expired
const cachedResponse = await getCachedResponse(DYNAMIC_CACHE, request);
if (cachedResponse) {
return cachedResponse;
}
return createOfflineFallback(request);
}
}
/**
* Handle dynamic requests (default strategy)
*/
async function handleDynamicRequest(request) {
try {
const networkResponse = await fetch(request);
if (networkResponse.ok) {
const cache = await caches.open(DYNAMIC_CACHE);
cache.put(request, networkResponse.clone());
}
return networkResponse;
} catch (error) {
console.log('Dynamic request failed, trying cache:', error.message);
const cachedResponse = await getCachedResponse(DYNAMIC_CACHE, request);
if (cachedResponse) {
return cachedResponse;
}
return createOfflineFallback(request);
}
}
/**
* Get cached response from specified cache
*/
async function getCachedResponse(cacheName, request) {
try {
const cache = await caches.open(cacheName);
return await cache.match(request);
} catch (error) {
console.warn('Error accessing cache:', error);
return null;
}
}
/**
* Check if cached response is expired
*/
function isCacheExpired(response, maxAge) {
const cachedAt = response.headers.get('sw-cached-at');
if (!cachedAt) return false;
const age = Date.now() - parseInt(cachedAt);
return age > maxAge;
}
/**
* Create offline fallback response
*/
function createOfflineFallback(request) {
const url = new URL(request.url);
if (url.pathname.endsWith('.html') || url.pathname === '/') {
return createOfflinePageResponse();
}
if (url.pathname.endsWith('.js')) {
return createOfflineJSResponse();
}
if (url.pathname.endsWith('.css')) {
return createOfflineCSSResponse();
}
return new Response('Offline - Resource not available', {
status: 503,
statusText: 'Service Unavailable',
headers: { 'Content-Type': 'text/plain' }
});
}
/**
* Create offline page response
*/
function createOfflinePageResponse() {
const offlineHTML = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Offline - Image Converter</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.offline-container {
text-align: center;
max-width: 500px;
padding: 2rem;
}
.offline-icon {
font-size: 4rem;
margin-bottom: 1rem;
}
.offline-title {
font-size: 2rem;
margin-bottom: 1rem;
font-weight: 600;
}
.offline-message {
font-size: 1.1rem;
margin-bottom: 2rem;
opacity: 0.9;
line-height: 1.6;
}
.offline-actions {
display: flex;
gap: 1rem;
justify-content: center;
flex-wrap: wrap;
}
.offline-button {
padding: 0.75rem 1.5rem;
background: rgba(255, 255, 255, 0.2);
border: 1px solid rgba(255, 255, 255, 0.3);
border-radius: 8px;
color: white;
text-decoration: none;
font-weight: 500;
transition: all 0.3s ease;
cursor: pointer;
}
.offline-button:hover {
background: rgba(255, 255, 255, 0.3);
transform: translateY(-2px);
}
.cached-tools {
margin-top: 2rem;
padding: 1rem;
background: rgba(255, 255, 255, 0.1);
border-radius: 8px;
}
.cached-tools h3 {
margin-top: 0;
margin-bottom: 1rem;
}
.tool-list {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
justify-content: center;
}
.tool-link {
padding: 0.5rem 1rem;
background: rgba(255, 255, 255, 0.2);
border-radius: 6px;
color: white;
text-decoration: none;
font-size: 0.9rem;
transition: background 0.3s ease;
}
.tool-link:hover {
background: rgba(255, 255, 255, 0.3);
}
</style>
</head>
<body>
<div class="offline-container">
<div class="offline-icon">📱</div>
<h1 class="offline-title">You're Offline</h1>
<p class="offline-message">
No internet connection detected. Some features may be limited, but you can still use cached tools and view previously loaded content.
</p>
<div class="offline-actions">
<button class="offline-button" onclick="window.location.reload()">
Try Again
</button>
<a href="/" class="offline-button">
Go Home
</a>
</div>
<div class="cached-tools">
<h3>Available Offline Tools</h3>
<div class="tool-list">
<a href="/tools/image-converter/" class="tool-link">Image Converter</a>
<a href="/tools/pdf-merger/" class="tool-link">PDF Merger</a>
<a href="/tools/background-remover/" class="tool-link">Background Remover</a>
</div>
</div>
</div>
<script>
// Auto-retry connection every 30 seconds
setInterval(() => {
if (navigator.onLine) {
window.location.reload();
}
}, 30000);
// Listen for online event
window.addEventListener('online', () => {
window.location.reload();
});
</script>
</body>
</html>
`;
return new Response(offlineHTML, {
status: 200,
headers: { 'Content-Type': 'text/html' }
});
}
/**
* Create offline JavaScript response
*/
function createOfflineJSResponse() {
const offlineJS = `
// Offline fallback JavaScript
console.warn('JavaScript file not available offline');
// Provide basic offline functionality
window.isOffline = true;
// Show offline notification
if (typeof window.showToast === 'function') {
window.showToast('Some features are limited while offline', 'warning');
}
`;
return new Response(offlineJS, {
status: 200,
headers: { 'Content-Type': 'application/javascript' }
});
}
/**
* Create offline CSS response
*/
function createOfflineCSSResponse() {
const offlineCSS = `
/* Offline fallback styles */
.offline-indicator {
position: fixed;
top: 0;
left: 0;
right: 0;
background: #f59e0b;
color: white;
text-align: center;
padding: 0.5rem;
font-size: 0.9rem;
z-index: 10000;
}
`;
return new Response(offlineCSS, {
status: 200,
headers: { 'Content-Type': 'text/css' }
});
}
/**
* Create offline API response
*/
function createOfflineAPIResponse(url) {
const offlineData = {
error: 'offline',
message: 'This feature requires an internet connection',
offline: true,
timestamp: Date.now()
};
return new Response(JSON.stringify(offlineData), {
status: 503,
statusText: 'Service Unavailable',
headers: { 'Content-Type': 'application/json' }
});
}
/**
* Create image placeholder
*/
function createImagePlaceholder() {
// Simple SVG placeholder
const placeholder = `
<svg width="300" height="200" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" fill="#f3f4f6"/>
<text x="50%" y="50%" text-anchor="middle" dy=".3em" fill="#6b7280" font-family="Arial, sans-serif" font-size="16">
Image not available offline
</text>
</svg>
`;
return new Response(placeholder, {
status: 200,
headers: { 'Content-Type': 'image/svg+xml' }
});
}
/**
* Helper functions to determine request types
*/
function isAPIRequest(url) {
return url.pathname.startsWith('/api/') ||
url.hostname.includes('supabase') ||
API_ENDPOINTS.some(endpoint => url.pathname.startsWith(endpoint));
}
function isStaticAsset(url) {
return STATIC_ASSETS.some(asset => url.pathname === asset || url.pathname.endsWith(asset));
}
function isImageRequest(url) {
return /\.(jpg|jpeg|png|gif|webp|svg|ico)$/i.test(url.pathname);
}
function isNetworkFirst(url) {
return NETWORK_FIRST.some(pattern => url.pathname.startsWith(pattern));
}
function isCacheFirst(url) {
return CACHE_FIRST.some(pattern => url.pathname.startsWith(pattern));
}
/**
* Background sync for offline actions
*/
self.addEventListener('sync', (event) => {
console.log('Background sync triggered:', event.tag);
if (event.tag === 'upload-files') {
event.waitUntil(syncOfflineUploads());
} else if (event.tag === 'sync-usage') {
event.waitUntil(syncUsageData());
}
});
/**
* Sync offline uploads when connection is restored
*/
async function syncOfflineUploads() {
try {
// Get offline uploads from IndexedDB
const offlineUploads = await getOfflineUploads();
for (const upload of offlineUploads) {
try {
// Attempt to upload file
const response = await fetch('/api/upload', {
method: 'POST',
body: upload.formData
});
if (response.ok) {
// Remove from offline storage
await removeOfflineUpload(upload.id);
console.log('Synced offline upload:', upload.filename);
}
} catch (error) {
console.error('Failed to sync upload:', error);
}
}
} catch (error) {
console.error('Background sync failed:', error);
}
}
/**
* Sync usage data when connection is restored
*/
async function syncUsageData() {
try {
// Get offline usage data
const offlineUsage = await getOfflineUsage();
if (offlineUsage.length > 0) {
const response = await fetch('/api/usage/sync', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(offlineUsage)
});
if (response.ok) {
await clearOfflineUsage();
console.log('Synced offline usage data');
}
}
} catch (error) {
console.error('Usage sync failed:', error);
}
}
/**
* IndexedDB helpers for offline storage
*/
async function getOfflineUploads() {
// Implementation would use IndexedDB to store offline uploads
return [];
}
async function removeOfflineUpload(id) {
// Implementation would remove upload from IndexedDB
}
async function getOfflineUsage() {
// Implementation would get usage data from IndexedDB
return [];
}
async function clearOfflineUsage() {
// Implementation would clear usage data from IndexedDB
}
/**
* Push notification handling
*/
self.addEventListener('push', (event) => {
if (!event.data) return;
const data = event.data.json();
const options = {
body: data.body,
icon: '/icons/icon-192x192.png',
badge: '/icons/badge-72x72.png',
tag: data.tag || 'default',
data: data.data || {},
actions: data.actions || []
};
event.waitUntil(
self.registration.showNotification(data.title, options)
);
});
/**
* Notification click handling
*/
self.addEventListener('notificationclick', (event) => {
event.notification.close();
const data = event.notification.data;
const action = event.action;
if (action === 'open-dashboard') {
event.waitUntil(
clients.openWindow('/dashboard.html')
);
} else if (data.url) {
event.waitUntil(
clients.openWindow(data.url)
);
}
});
console.log('Service Worker loaded successfully');