Repository navigation
Expand file tree
/
Copy pathservice-worker.js
More file actions
145 lines (133 loc) · 4.23 KB
/
Copy pathservice-worker.js
File metadata and controls
145 lines (133 loc) · 4.23 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
/**
* Idolon Chat Service Worker
*
* Caching Strategy:
* - For Navigation (HTML pages): Network First, falling back to Cache.
* This ensures users always get the latest app shell when online.
* - For all other assets: Cache First, falling back to Network.
* This provides the best performance for static assets like images and icons.
*/
// IMPORTANT: Increment this version whenever you deploy updates.
const CACHE_NAME = 'idolon-chat-cache-v37'; // Change to v33 for your next update
const urlsToCache = [
// Core HTML files
'/',
'/index.html',
'/help.html',
// Manifest and core favicons
'/manifest.json',
'/favicon.ico',
'/favicon.svg',
'/favicon.png',
// Default character images specified in the initial app state
'/char_imgs/idolon.jpg',
'/char_imgs/gremlin.jpg',
'/char_imgs/olivia.jpg',
'/char_imgs/kaelan.jpg',
// Key PWA icons
'/android/android-launchericon-512-512.png',
'/android/android-launchericon-192-192.png',
'/ios/192.png',
'/ios/512.png',
'/windows11/Square150x150Logo.scale-200.png',
'/windows11/SplashScreen.scale-200.png',
// Screenshots
'/screenshots/1ss.jpg',
'/screenshots/desktop-ss.jpg'
];
/**
* INSTALL: Cache the application shell.
*/
self.addEventListener('install', event => {
console.log('[Service Worker] Install event fired.');
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('[Service Worker] Caching App Shell...');
return cache.addAll(urlsToCache);
})
.catch(error => {
console.error('[Service Worker] Failed to cache App Shell:', error);
})
);
});
/**
* ACTIVATE: Clean up old caches.
*/
self.addEventListener('activate', event => {
console.log('[Service Worker] Activate event fired.');
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cache => {
if (cache !== CACHE_NAME) {
console.log('[Service Worker] Clearing old cache:', cache);
return caches.delete(cache);
}
})
);
})
);
return self.clients.claim();
});
/**
* FETCH: Intercept network requests with our new hybrid strategy.
*/
self.addEventListener('fetch', event => {
// We only cache GET requests
if (event.request.method !== 'GET') {
return;
}
// --- STRATEGY 1: Network First for Navigation ---
// If the request is for a page navigation (e.g., loading index.html).
if (event.request.mode === 'navigate') {
event.respondWith(
fetch(event.request)
.then(networkResponse => {
// If the network request is successful, cache the new response.
if (networkResponse && networkResponse.status === 200) {
const responseToCache = networkResponse.clone();
caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, responseToCache);
});
}
return networkResponse;
})
.catch(() => {
// If the network fails (offline), serve the page from the cache.
console.log('[Service Worker] Network fetch failed for navigation. Serving from cache.');
return caches.match(event.request);
})
);
return; // End execution for navigation requests.
}
// --- STRATEGY 2: Cache First for All Other Assets ---
// For images, scripts, styles, etc.
event.respondWith(
caches.match(event.request).then(cachedResponse => {
// If the asset is in the cache, return it.
if (cachedResponse) {
return cachedResponse;
}
// Otherwise, fetch it from the network, cache it, and return it.
return fetch(event.request).then(networkResponse => {
if (networkResponse && networkResponse.status === 200) {
const responseToCache = networkResponse.clone();
caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, responseToCache);
});
}
return networkResponse;
});
})
);
});
/**
* MESSAGE: Listen for the 'skipWaiting' command from the app.
*/
self.addEventListener('message', event => {
if (event.data && event.data.action === 'skipWaiting') {
console.log('[Service Worker] skipWaiting command received. Activating new version.');
self.skipWaiting();
}
});