-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
137 lines (125 loc) · 4.67 KB
/
Copy pathsw.js
File metadata and controls
137 lines (125 loc) · 4.67 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
// Service Worker for Caching and Performance
// Update this version number on each deployment to force cache refresh
const SW_VERSION = '2.0.0';
const CACHE_NAME = `algorithm-visualizer-${SW_VERSION}`;
const STATIC_CACHE = `static-${SW_VERSION}`;
const ICON_CACHE = `icons-${SW_VERSION}`;
const BASE_PATH = '';
// Assets to cache immediately
const STATIC_ASSETS = [
`${BASE_PATH}/`,
`${BASE_PATH}/index.html`,
`${BASE_PATH}/styles.css`,
`${BASE_PATH}/main.js`,
`${BASE_PATH}/visualizer.js`,
`${BASE_PATH}/audio.js`,
`${BASE_PATH}/algorithms.js`
];
// Install event - cache static assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(STATIC_CACHE).then((cache) => {
return cache.addAll(STATIC_ASSETS).catch((err) => {
console.log('Cache addAll failed:', err);
});
})
);
// Force the waiting service worker to become the active service worker
self.skipWaiting();
});
// Activate event - clean up old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => {
// Delete all caches that don't match current version
return (name !== STATIC_CACHE && name !== ICON_CACHE) &&
(name.startsWith('algorithm-visualizer-') || name.startsWith('static-') || name.startsWith('icons-'));
})
.map((name) => {
console.log('Deleting old cache:', name);
return caches.delete(name);
})
);
}).then(() => {
// Take control of all pages immediately
return self.clients.claim();
})
);
});
// Fetch event - network-first for HTML, stale-while-revalidate for assets
self.addEventListener('fetch', (event) => {
const { request } = event;
const url = new URL(request.url);
// Skip non-GET requests
if (request.method !== 'GET') {
return;
}
// Network-first strategy for HTML files (always get fresh version)
if (url.pathname === '/' || url.pathname === '/index.html' || url.pathname.endsWith('.html')) {
event.respondWith(
fetch(request)
.then((response) => {
// Clone the response before caching
const responseToCache = response.clone();
caches.open(STATIC_CACHE).then((cache) => {
cache.put(request, responseToCache);
});
return response;
})
.catch(() => {
// Fallback to cache if network fails
return caches.match(request);
})
);
return;
}
// Cache-first for icons (they rarely change)
if (url.pathname.includes('/icons/')) {
event.respondWith(
caches.open(ICON_CACHE).then((cache) => {
return cache.match(request).then((response) => {
if (response) {
return response;
}
return fetch(request).then((fetchResponse) => {
if (fetchResponse.ok) {
cache.put(request, fetchResponse.clone());
}
return fetchResponse;
});
});
})
);
return;
}
// Stale-while-revalidate for CSS and JS files
if (url.pathname.endsWith('.css') || url.pathname.endsWith('.js')) {
event.respondWith(
caches.open(STATIC_CACHE).then((cache) => {
return cache.match(request).then((cachedResponse) => {
// Always fetch in background to update cache
const fetchPromise = fetch(request).then((networkResponse) => {
if (networkResponse.ok) {
cache.put(request, networkResponse.clone());
}
return networkResponse;
}).catch(() => {
// Network failed, ignore
});
// Return cached version immediately if available, otherwise wait for network
return cachedResponse || fetchPromise;
});
})
);
return;
}
// Network first for other requests
event.respondWith(
fetch(request).catch(() => {
return caches.match(request);
})
);
});