|
1 | 1 | const appPrefix = 'TrackGen'; |
2 | 2 | const appVersion = 'v1.0.9'; |
3 | 3 | const cacheName = `${appPrefix}-${appVersion}`; |
4 | | -const foldersToCache = ['media', 'js', 'css']; |
5 | | -const additionalCache = ['/', 'manifest.json', 'index.html']; |
6 | 4 |
|
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 | +]; |
36 | 39 |
|
37 | 40 | function isImage(request) { |
38 | 41 | return request.destination === 'image' || request.url.match(/\.(png|jpg|jpeg|webp|gif)$/i); |
39 | 42 | } |
40 | 43 |
|
41 | 44 | function isCachable(request) { |
42 | 45 | 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); |
44 | 47 | } |
45 | 48 |
|
46 | 49 | async function staleWhileRevalidate(request) { |
@@ -81,12 +84,10 @@ async function cacheFirstWithRefresh(request) { |
81 | 84 |
|
82 | 85 | self.addEventListener('install', event => { |
83 | 86 | 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); |
88 | 89 | }).catch(error => { |
89 | | - console.error('Failed to generate files to cache:', error); |
| 90 | + console.error('Failed to cache files during install:', error); |
90 | 91 | }) |
91 | 92 | ); |
92 | 93 | }); |
|
0 commit comments