forked from EliasPereirah/OrionChat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
133 lines (120 loc) · 4.45 KB
/
service-worker.js
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
const CACHE_VERSION = 'v1.1.6';
const CACHE_NAME = `orion_cache_${CACHE_VERSION}`;
const STATIC_ASSETS = [
'./index.html',
'./',
'./css/chat.css',
'./css/highlight_js/themes/github-dark-dimmed.css',
'./favicon.png',
'./imgs/icons/orion_192x192.png',
'./imgs/new-chat.png',
'./imgs/screenshot.png',
'./imgs/settings.png',
'./js/4devs.js',
'./js/google_cse.js',
'./js/library/highlight.js',
'./js/library/showdown@1.9.0.js',
'./js/library/showdown@2.1.0.js',
'./js/md5.js',
'./js/prompts.js',
'./js/script.js',
'./js/stt.js',
'./js/tools_list.js',
'./js/tts.js',
'./manifest.json'
];
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('Caching static assets...');
return Promise.all(
STATIC_ASSETS.map(url => {
return cache.add(url).catch(err => {
console.error(`Failed to cache ${url}:`, err);
});
})
);
})
.then(() => {
console.log('Static assets cached successfully');
return self.skipWaiting();
})
);
});
self.addEventListener('activate', event => {
event.waitUntil(
Promise.all([
caches.keys()
.then(cacheNames => {
return Promise.all(
cacheNames
.filter(cacheName => {
return cacheName.startsWith('orion_cache_') && cacheName !== CACHE_NAME;
})
.map(cacheName => {
console.log('Removing old cache:', cacheName);
return caches.delete(cacheName);
})
);
}),
self.clients.claim()
])
);
});
self.addEventListener('fetch', event => {
if (event.request.method !== 'GET') {
return;
}
// Create a clean URL for matching
const requestURL = new URL(event.request.url);
const cleanURL = requestURL.pathname.replace(/^\//, ''); // Remove leading slash
event.respondWith(
caches.match(event.request)
.then(cachedResponse => {
if (cachedResponse) {
// Return cached response immediately
return cachedResponse;
}
return fetch(event.request)
.then(networkResponse => {
if (!networkResponse || !networkResponse.ok) {
throw new Error('Network response was not ok');
}
// Check if this is a static asset that should be cached
const shouldCache = STATIC_ASSETS.some(asset => {
const cleanAsset = asset.replace(/^\.\//, ''); // Remove leading ./
return cleanURL.endsWith(cleanAsset);
});
if (shouldCache) {
const responseToCache = networkResponse.clone();
caches.open(CACHE_NAME)
.then(cache => {
cache.put(event.request, responseToCache);
})
.catch(err => {
console.error('Failed to cache response:', err);
});
}
return networkResponse;
});
})
.catch(error => {
console.error('Fetch handler failed:', error);
if (event.request.mode === 'navigate') {
return caches.match('./index.html');
}
return new Response('Network error', {
status: 408,
headers: new Headers({
'Content-Type': 'text/plain'
})
});
})
);
});
self.addEventListener('message', event => {
if (event.data === 'skipWaiting') {
self.skipWaiting();
}
});