-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsw.js
More file actions
89 lines (83 loc) · 2.79 KB
/
Copy pathsw.js
File metadata and controls
89 lines (83 loc) · 2.79 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
const CACHE_NAME = 'lensing-viz-v1.1';
const ASSETS_TO_CACHE = [
'./index.html',
'./css/styles.css',
'./js/utils.js',
'./js/galaxy-factory.js',
'./js/textures.js',
'./js/shaders.js',
'./js/ui.js',
'./js/app.js',
'./manifest.json',
'./icons/icon-192x192.png',
'./icons/icon-512x512.png',
'https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js'
];
// Install event - cache assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
return cache.addAll(ASSETS_TO_CACHE);
})
.then(() => {
// Activate immediately
return self.skipWaiting();
})
);
});
// Activate event - clean up old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys()
.then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => name !== CACHE_NAME)
.map((name) => caches.delete(name))
);
})
.then(() => {
// Take control of all pages immediately
return self.clients.claim();
})
);
});
// Fetch event - serve from cache, fall back to network
self.addEventListener('fetch', (event) => {
// Skip non-GET requests
if (event.request.method !== 'GET') {
return;
}
event.respondWith(
fetch(event.request)
.then((networkResponse) => {
// 1. Network Succeeded:
// Check if we received a valid response
if (!networkResponse || networkResponse.status !== 200 || networkResponse.type !== 'basic') {
return networkResponse;
}
// 2. Update the cache with the new file
const responseToCache = networkResponse.clone();
caches.open(CACHE_NAME)
.then((cache) => {
cache.put(event.request, responseToCache);
});
return networkResponse;
})
.catch(() => {
// 3. Network Failed (Offline):
// Fallback to cache
return caches.match(event.request)
.then((cachedResponse) => {
if (cachedResponse) {
return cachedResponse;
}
// Optional: Return a specific offline page if the file isn't found
if (event.request.mode === 'navigate') {
return caches.match('./index.html');
}
});
})
);
});