-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.bjs
More file actions
203 lines (185 loc) · 5.36 KB
/
sw.bjs
File metadata and controls
203 lines (185 loc) · 5.36 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
const CACHE_NAME = 'we-os-v2';
const STATIC_CACHE = 'we-os-static-v2';
const DYNAMIC_CACHE = 'we-os-dynamic-v2';
const CORE_ASSETS = [
'/',
'/index.html',
'/manifest.json',
'/favicon.ico',
'/offline.html',
'/chat.html'
];
// Install event - cache core assets
self.addEventListener('install', (event) => {
console.log('Service worker installing...');
event.waitUntil(
caches.open(STATIC_CACHE)
.then((cache) => {
console.log('Opened cache');
return cache.addAll(CORE_ASSETS);
})
.then(() => {
console.log('Core assets cached');
return self.skipWaiting();
})
);
});
// Activate event - clean up 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 !== STATIC_CACHE && cacheName !== DYNAMIC_CACHE) {
console.log('Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
}).then(() => {
return self.clients.claim();
})
);
});
// Fetch event - implement cache strategies
self.addEventListener('fetch', (event) => {
const { request } = event;
const url = new URL(request.url);
// Skip cross-origin requests
if (url.origin !== location.origin) {
return;
}
// Handle navigation requests
if (request.mode === 'navigate') {
event.respondWith(
caches.match(request)
.then((response) => {
if (response) {
return response;
}
return fetch(request)
.then((fetchResponse) => {
const responseClone = fetchResponse.clone();
caches.open(DYNAMIC_CACHE)
.then((cache) => {
cache.put(request, responseClone);
});
return fetchResponse;
})
.catch(() => {
return caches.match('/offline.html');
});
})
);
return;
}
// --- Bypass Archie Bridge (SSE and API) ---
if (request.url.includes('/clawd-bridge/')) {
return; // Let the browser handle it directly
}
// Handle other requests with cache-first strategy for static assets
if (request.url.includes('/desktop/') ||
request.url.includes('/css/') ||
request.url.includes('/js/') ||
request.url.includes('/images/') ||
request.url.includes('.js') ||
request.url.includes('.css')) {
event.respondWith(
caches.match(request)
.then((response) => {
if (response) {
return response;
}
return fetch(request)
.then((fetchResponse) => {
const responseClone = fetchResponse.clone();
caches.open(STATIC_CACHE)
.then((cache) => {
cache.put(request, responseClone);
});
return fetchResponse;
});
})
);
return;
}
// Handle API calls with network-first strategy
if (request.url.includes('/api/')) {
event.respondWith(
fetch(request)
.then((fetchResponse) => {
const responseClone = fetchResponse.clone();
caches.open(DYNAMIC_CACHE)
.then((cache) => {
cache.put(request, responseClone);
});
return fetchResponse;
})
.catch(() => {
return caches.match(request);
})
);
return;
}
// Default strategy for other requests
event.respondWith(
caches.match(request)
.then((response) => {
return response || fetch(request);
})
);
});
// Background sync for offline actions
self.addEventListener('sync', (event) => {
if (event.tag === 'background-sync') {
event.waitUntil(
// Handle any background sync operations
console.log('Background sync triggered')
);
}
});
// --- Archie Real-time Messages (SSE via Service Worker) ---
// This allows the Service Worker to listen for messages even if no tabs are active
let eventSource = null;
function connectToBridge() {
if (eventSource) eventSource.close();
// Note: EventSource in SW requires specific handling or a long-poll fallback.
// For now, we will handle messages via the Push API when available,
// or by keeping the SW alive while messages are expected.
}
// Push notification handling
self.addEventListener('push', (event) => {
const data = event.data ? event.data.json() : {};
const title = data.title || 'Archie';
const options = {
body: data.message || 'New message from Archie',
icon: '/icons/icon-192.png',
badge: '/icons/icon-192.png',
vibrate: [200, 100, 200],
tag: 'archie-msg',
renotify: true,
data: {
url: '/mobile.html'
}
};
event.waitUntil(
self.registration.showNotification(title, options)
);
});
// Notification click handling
self.addEventListener('notificationclick', (event) => {
event.notification.close();
event.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true }).then(clientList => {
for (const client of clientList) {
if (client.url.includes('/mobile.html') && 'focus' in client) {
return client.focus();
}
}
if (clients.openWindow) {
return clients.openWindow('/mobile.html');
}
})
);
});