Skip to content

Commit a295825

Browse files
Remove dynamic file discovery from sw
causes errors at first load
1 parent ab3828a commit a295825

1 file changed

Lines changed: 38 additions & 37 deletions

File tree

service-worker.js

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
11
const appPrefix = 'TrackGen';
22
const appVersion = 'v1.0.9';
33
const cacheName = `${appPrefix}-${appVersion}`;
4-
const foldersToCache = ['media', 'js', 'css'];
5-
const additionalCache = ['/', 'manifest.json', 'index.html'];
64

7-
let filesToCache = [];
8-
9-
async function generateFilesToCache() {
10-
filesToCache = [];
11-
12-
for (const folder of foldersToCache) {
13-
try {
14-
const response = await fetch(`/${folder}/`);
15-
if (!response.ok) {
16-
console.error(`Failed to fetch ${folder} folder: ${response.statusText}`);
17-
continue;
18-
}
19-
const folderFiles = await response.text();
20-
const regex = /href="([^"]+\.(png|jpg|jpeg|jxl|webp|js|css))"/g;
21-
let match;
22-
23-
while ((match = regex.exec(folderFiles)) !== null) {
24-
if (match[1] !== 'bg16383.webp') {
25-
filesToCache.push(`/${folder}/${match[1]}`);
26-
}
27-
}
28-
} catch (error) {
29-
console.error(`Error fetching files from ${folder}:`, error);
30-
}
31-
}
32-
33-
filesToCache.push(...additionalCache);
34-
return filesToCache;
35-
}
5+
// static list of files to cache
6+
const filesToCache = [
7+
'/',
8+
'/index.html',
9+
'/manifest.webmanifest',
10+
11+
// CSS
12+
'/static/css/style.css',
13+
14+
// JS
15+
'/static/js/sw.js',
16+
'/static/js/pages.js',
17+
'/static/js/generate.js',
18+
'/static/js/new_point.js',
19+
'/static/js/atcf.js',
20+
'/static/js/rsmc.js',
21+
'/static/js/hurdat.js',
22+
'/static/js/ibtracs.js',
23+
'/static/js/storms.js',
24+
'/static/js/file_upload.js',
25+
'/static/js/manual_input.js',
26+
'/static/js/export.js',
27+
'/static/js/export-hurdat.js',
28+
29+
// media (exclude large maps)
30+
'/static/media/favicon.png',
31+
'/static/media/cyclone.png',
32+
'/static/media/background.png',
33+
'/static/media/background-dark.png',
34+
'/static/media/bg8192.png',
35+
'/static/media/bg12000.jpg',
36+
'/static/media/bg13500-blkmar.jpg',
37+
'/static/media/bg21600-nxtgen.jpg'
38+
];
3639

3740
function isImage(request) {
3841
return request.destination === 'image' || request.url.match(/\.(png|jpg|jpeg|webp|gif)$/i);
3942
}
4043

4144
function isCachable(request) {
4245
const url = new URL(request.url);
43-
return filesToCache && url.origin === location.origin && filesToCache.includes(url.pathname);
46+
return url.origin === location.origin && filesToCache.includes(url.pathname);
4447
}
4548

4649
async function staleWhileRevalidate(request) {
@@ -81,12 +84,10 @@ async function cacheFirstWithRefresh(request) {
8184

8285
self.addEventListener('install', event => {
8386
event.waitUntil(
84-
generateFilesToCache().then(files => {
85-
return caches.open(cacheName).then(cache => {
86-
return cache.addAll(files);
87-
});
87+
caches.open(cacheName).then(cache => {
88+
return cache.addAll(filesToCache);
8889
}).catch(error => {
89-
console.error('Failed to generate files to cache:', error);
90+
console.error('Failed to cache files during install:', error);
9091
})
9192
);
9293
});

0 commit comments

Comments
 (0)