-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
135 lines (118 loc) · 4.44 KB
/
Copy pathsw.js
File metadata and controls
135 lines (118 loc) · 4.44 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
/**
* Face Morph Pro - Service Worker
* Enables PWA installation and offline caching
*/
const CACHE_NAME = 'face-morph-pro-v1';
const STATIC_CACHE = 'face-morph-static-v1';
// Files to cache for offline use
const STATIC_FILES = [
'/',
'/index.html',
'/styles.css',
'/app.js',
'/morph-engine.js',
'/manifest.json',
'/static/icon-192.png',
'/static/icon-512.png'
];
// CDN resources (cached on first use)
const CDN_RESOURCES = [
'https://cdn.jsdelivr.net/npm/@mediapipe/face_mesh/face_mesh.js',
'https://cdn.jsdelivr.net/npm/@mediapipe/camera_utils/camera_utils.js',
'https://cdn.jsdelivr.net/npm/face-api.js@0.22.2/dist/face-api.min.js',
'https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap'
];
// Install event - cache static files
self.addEventListener('install', (event) => {
console.log('[ServiceWorker] Installing...');
event.waitUntil(
caches.open(STATIC_CACHE)
.then((cache) => {
console.log('[ServiceWorker] Caching static files');
return cache.addAll(STATIC_FILES);
})
.then(() => {
console.log('[ServiceWorker] Install complete');
return self.skipWaiting();
})
.catch((error) => {
console.error('[ServiceWorker] Install failed:', error);
})
);
});
// Activate event - clean up old caches
self.addEventListener('activate', (event) => {
console.log('[ServiceWorker] Activating...');
event.waitUntil(
caches.keys()
.then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => name !== STATIC_CACHE && name !== CACHE_NAME)
.map((name) => {
console.log('[ServiceWorker] Deleting old cache:', name);
return caches.delete(name);
})
);
})
.then(() => {
console.log('[ServiceWorker] Activation complete');
return self.clients.claim();
})
);
});
// Fetch event - serve from cache, fallback to network
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
// Skip non-GET requests
if (event.request.method !== 'GET') {
return;
}
// Skip chrome-extension and other non-http requests
if (!url.protocol.startsWith('http')) {
return;
}
event.respondWith(
caches.match(event.request)
.then((cachedResponse) => {
if (cachedResponse) {
// Return cached response
return cachedResponse;
}
// Fetch from network
return fetch(event.request)
.then((networkResponse) => {
// Don't cache if not a valid response
if (!networkResponse || networkResponse.status !== 200) {
return networkResponse;
}
// Clone the response for caching
const responseToCache = networkResponse.clone();
// Cache CDN resources and morph images
if (CDN_RESOURCES.some(cdn => event.request.url.includes(cdn)) ||
event.request.url.includes('/morph_images/')) {
caches.open(CACHE_NAME)
.then((cache) => {
cache.put(event.request, responseToCache);
});
}
return networkResponse;
})
.catch((error) => {
console.error('[ServiceWorker] Fetch failed:', error);
// Return offline fallback for navigation requests
if (event.request.mode === 'navigate') {
return caches.match('/index.html');
}
return new Response('Offline', { status: 503 });
});
})
);
});
// Handle messages from the main app
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});
console.log('[ServiceWorker] Script loaded');