commit b539ea085869998dc2f7788a8981d73b468469a3
parent f534fbb3aeda0c62e48ff10100430217877f3a12
Author: Hunter
Date: Tue, 7 Apr 2026 01:04:16 -0400
don't use cache on localhost; improve offline reliability
Diffstat:
2 files changed, 21 insertions(+), 6 deletions(-)
diff --git a/build.py b/build.py
@@ -103,8 +103,8 @@ def _next_cache_name(app_name, manifest_path):
return f"{app_name}-v1"
-def generate_pwa_manifests(app_name=None, base_path=None):
- """Generate PWA manifest files based on tracks.json
+def build_pwa(app_name=None, base_path=None):
+ """Generate manifest.json and service-worker.js based on tracks.json
Args:
app_name: Name of the app. If None, will be prompted via get_configuration()
@@ -119,7 +119,7 @@ def generate_pwa_manifests(app_name=None, base_path=None):
cache_name = _next_cache_name(app_name, SCRIPT_DIR / "manifest.json")
app_description = f"{app_name}"
- print("Generating PWA manifests...")
+ print("Building PWA files...")
print(f" Cache name: {cache_name}")
# Load tracks.json
@@ -309,10 +309,10 @@ self.addEventListener('fetch', (event) => {{
f.write(service_worker_content)
print("✓ Generated service-worker.js")
print()
- print("PWA manifests generated successfully!")
+ print("PWA build complete!")
if __name__ == "__main__":
- # When run directly, get configuration and generate manifests
+ # When run directly, get configuration and build PWA files
app_name, base_path = get_configuration()
- generate_pwa_manifests(app_name, base_path)
+ build_pwa(app_name, base_path)
diff --git a/resources/script.js b/resources/script.js
@@ -9,6 +9,14 @@ if ('serviceWorker' in navigator && location.hostname !== 'localhost' && locatio
console.log('Service Worker registration failed:', error);
});
});
+} else if ('serviceWorker' in navigator && (location.hostname === 'localhost' || location.hostname === '127.0.0.1')) {
+ // On localhost, unregister any existing service worker and clear caches
+ navigator.serviceWorker.getRegistrations().then(registrations => {
+ registrations.forEach(r => r.unregister());
+ });
+ caches.keys().then(keys => {
+ keys.forEach(k => caches.delete(k));
+ });
}
const playPauseBtn = document.getElementById('playPause');
@@ -84,6 +92,7 @@ fetch('manifest.json')
if (!response.ok) return null;
return response.json();
})
+ .catch(() => null)
.then(manifest => {
if (manifest) {
CACHE_NAME = manifest.cache_name || manifest.name || CACHE_NAME;
@@ -107,6 +116,12 @@ fetch('manifest.json')
if (!r.ok) throw new Error('tracks.json not found');
return r.json();
})
+ .catch(() => {
+ // Offline fallback: try loading from cache directly
+ return caches.open(CACHE_NAME)
+ .then(cache => cache.match('mix/tracks.json'))
+ .then(r => r ? r.json() : Promise.reject('tracks.json not in cache'));
+ })
]);
})
.then((results) => {