-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
278 lines (247 loc) · 7.48 KB
/
sw.js
File metadata and controls
278 lines (247 loc) · 7.48 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Service Worker for Miles Waite Portfolio PWA
const CACHE_NAME = 'miles-waite-portfolio-v1.0.0';
const STATIC_CACHE = 'static-v1.0.0';
const DYNAMIC_CACHE = 'dynamic-v1.0.0';
// Files to cache immediately
const STATIC_FILES = [
'/',
'/index.html',
'/about.html',
'/style.css',
'/script.js',
'/tooltips.js',
'/favicon-32x32.png',
'/manifest.json',
'/offline.html',
// Project pages
'/audio-reactive-abstract-geometry.html',
'/audio-reactive-visuals.html',
'/creative-coding.html',
'/cyberpunk-network.html',
'/generative-max-for-live-tools.html',
'/generative-music.html',
'/live-performance.html',
'/max-for-live-tools.html',
'/particle-systems.html',
'/python.html',
'/systems-architecture.html',
'/touchdesigner.html',
'/api-text-dat.html',
// Edge rendering
'/edge-rendering/edge-rendering.html',
'/edge-rendering/edge-rendering.js',
'/edge-rendering/edge-renderer-worker.js',
'/edge-rendering/webgpu-edge-renderer.js',
// External resources
'https://fonts.googleapis.com/css2?family=Inter:wght@100..900&display=swap',
'https://cdn.jsdelivr.net/npm/p5@1.6.0/lib/p5.min.js'
];
// Files to cache on demand
const DYNAMIC_FILES = [
'/assets/',
'/images/',
'/videos/',
'/audio/',
'/docs/'
];
// Install event - cache static files
self.addEventListener('install', event => {
console.log('Service Worker: Installing...');
event.waitUntil(
caches.open(STATIC_CACHE)
.then(cache => {
console.log('Service Worker: Caching static files');
return cache.addAll(STATIC_FILES);
})
.then(() => {
console.log('Service Worker: Static files cached successfully');
return self.skipWaiting();
})
.catch(error => {
console.error('Service Worker: Error caching static files:', error);
})
);
});
// 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('Service Worker: Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
})
.then(() => {
console.log('Service Worker: Activated successfully');
return self.clients.claim();
})
);
});
// Fetch event - serve from cache, fallback to network
self.addEventListener('fetch', event => {
const { request } = event;
const url = new URL(request.url);
// Skip non-GET requests
if (request.method !== 'GET') {
return;
}
// Skip chrome-extension and other non-http requests
if (!url.protocol.startsWith('http')) {
return;
}
event.respondWith(
caches.match(request)
.then(cachedResponse => {
// Return cached version if available
if (cachedResponse) {
console.log('Service Worker: Serving from cache:', request.url);
return cachedResponse;
}
// Otherwise, fetch from network
console.log('Service Worker: Fetching from network:', request.url);
return fetch(request)
.then(response => {
// Don't cache non-successful responses
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
// Clone the response
const responseToCache = response.clone();
// Cache dynamic content
if (shouldCache(request.url)) {
caches.open(DYNAMIC_CACHE)
.then(cache => {
cache.put(request, responseToCache);
});
}
return response;
})
.catch(error => {
console.log('Service Worker: Network error, serving offline page:', error);
// Return offline page for navigation requests
if (request.mode === 'navigate') {
return caches.match('/offline.html');
}
// Return a generic offline response for other requests
return new Response('Offline', {
status: 503,
statusText: 'Service Unavailable',
headers: new Headers({
'Content-Type': 'text/plain'
})
});
});
})
);
});
// Helper function to determine if URL should be cached
function shouldCache(url) {
// Cache assets and project files
if (url.includes('/assets/') ||
url.includes('/images/') ||
url.includes('/videos/') ||
url.includes('/audio/') ||
url.includes('.html') ||
url.includes('.css') ||
url.includes('.js') ||
url.includes('.png') ||
url.includes('.jpg') ||
url.includes('.jpeg') ||
url.includes('.gif') ||
url.includes('.svg') ||
url.includes('.webp')) {
return true;
}
// Don't cache external analytics or tracking
if (url.includes('google-analytics') ||
url.includes('googletagmanager') ||
url.includes('facebook.com') ||
url.includes('twitter.com')) {
return false;
}
return false;
}
// Background sync for form submissions (if needed in future)
self.addEventListener('sync', event => {
if (event.tag === 'background-sync') {
console.log('Service Worker: Background sync triggered');
event.waitUntil(doBackgroundSync());
}
});
function doBackgroundSync() {
// Handle any pending form submissions or data sync
return Promise.resolve();
}
// Push notification handling (for future use)
self.addEventListener('push', event => {
console.log('Service Worker: Push notification received');
const options = {
body: event.data ? event.data.text() : 'New content available on Miles Waite Portfolio',
icon: '/icons/icon-192x192.png',
badge: '/icons/icon-72x72.png',
vibrate: [100, 50, 100],
data: {
dateOfArrival: Date.now(),
primaryKey: 1
},
actions: [
{
action: 'explore',
title: 'View Portfolio',
icon: '/icons/icon-72x72.png'
},
{
action: 'close',
title: 'Close',
icon: '/icons/icon-72x72.png'
}
]
};
event.waitUntil(
self.registration.showNotification('Miles Waite Portfolio', options)
);
});
// Notification click handling
self.addEventListener('notificationclick', event => {
console.log('Service Worker: Notification clicked');
event.notification.close();
if (event.action === 'explore') {
event.waitUntil(
clients.openWindow('/')
);
} else if (event.action === 'close') {
// Just close the notification
return;
} else {
// Default action - open the portfolio
event.waitUntil(
clients.openWindow('/')
);
}
});
// Message handling for communication with main thread
self.addEventListener('message', event => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
if (event.data && event.data.type === 'GET_VERSION') {
event.ports[0].postMessage({ version: CACHE_NAME });
}
});
// Periodic background sync (if supported)
self.addEventListener('periodicsync', event => {
if (event.tag === 'content-sync') {
console.log('Service Worker: Periodic sync triggered');
event.waitUntil(updateContent());
}
});
function updateContent() {
// Check for updates to portfolio content
return Promise.resolve();
}