feat: add space weather image prefetch and stable cache-busting

Backend: Add /prefetch-images endpoint that warms the image cache in
parallel using a thread pool, skipping already-cached images.

Frontend: Trigger prefetch on mode init so images load instantly.
Replace per-request Date.now() cache-bust with a 5-minute rotating
key to allow browser caching aligned with backend max-age.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Smittix
2026-03-02 22:21:55 +00:00
parent 4b64862eb4
commit f07ec23da9
2 changed files with 42 additions and 2 deletions

View File

@@ -19,6 +19,11 @@ const SpaceWeather = (function () {
let _solarImageKey = 'sdo_193';
let _drapFreq = 'drap_global';
/** Stable cache-bust key that rotates every 5 minutes (matches backend max-age). */
function _cacheBust() {
return 'v=' + Math.floor(Date.now() / 300000);
}
// -------------------------------------------------------------------
// Public API
// -------------------------------------------------------------------
@@ -27,6 +32,8 @@ const SpaceWeather = (function () {
if (!_initialized) {
_initialized = true;
}
// Warm the backend image cache in parallel before rendering
fetch('/space-weather/prefetch-images').catch(function () {});
refresh();
_startAutoRefresh();
}
@@ -50,7 +57,7 @@ const SpaceWeather = (function () {
const img = new Image();
img.onload = function () { frame.innerHTML = ''; frame.appendChild(img); };
img.onerror = function () { frame.innerHTML = '<div class="sw-empty">Failed to load image</div>'; };
img.src = '/space-weather/image/' + key + '?t=' + Date.now();
img.src = '/space-weather/image/' + key + '?' + _cacheBust();
img.alt = key;
}
}
@@ -64,7 +71,7 @@ const SpaceWeather = (function () {
const img = new Image();
img.onload = function () { frame.innerHTML = ''; frame.appendChild(img); };
img.onerror = function () { frame.innerHTML = '<div class="sw-empty">Failed to load image</div>'; };
img.src = '/space-weather/image/' + key + '?t=' + Date.now();
img.src = '/space-weather/image/' + key + '?' + _cacheBust();
img.alt = key;
}
}