mirror of
https://github.com/kennethreitz/kjvstudy.org.git
synced 2026-06-05 23:00:16 +00:00
Show offline caching progress on page load
- Service worker now responds to GET_CACHE_STATUS message - /offline page queries caching status when it loads - If download is in progress, shows progress bar immediately without requiring click 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -314,4 +314,21 @@ self.addEventListener('message', (event) => {
|
||||
console.log('[SW] Received START_CACHING request');
|
||||
startBackgroundCaching();
|
||||
}
|
||||
|
||||
// Report current caching status
|
||||
if (event.data && event.data.type === 'GET_CACHE_STATUS') {
|
||||
if (cachingInProgress) {
|
||||
event.source.postMessage({
|
||||
type: 'CACHE_PROGRESS',
|
||||
cached: cachedCount,
|
||||
total: totalToCache,
|
||||
inProgress: true
|
||||
});
|
||||
} else {
|
||||
event.source.postMessage({
|
||||
type: 'CACHE_IDLE',
|
||||
inProgress: false
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -498,23 +498,39 @@
|
||||
navigator.serviceWorker.addEventListener('message', function(event) {
|
||||
const data = event.data;
|
||||
if (data.type === 'CACHE_STATUS') {
|
||||
cacheProgress.classList.add('active');
|
||||
progressText.textContent = data.status;
|
||||
} else if (data.type === 'CACHE_PROGRESS') {
|
||||
// Show progress bar if caching is in progress
|
||||
cacheProgress.classList.add('active');
|
||||
startCacheBtn.disabled = true;
|
||||
startCacheBtn.textContent = 'Downloading...';
|
||||
|
||||
const pct = Math.round((data.cached / data.total) * 100);
|
||||
progressFill.style.width = pct + '%';
|
||||
progressText.textContent = 'Downloading: ' + data.cached.toLocaleString() + ' / ' + data.total.toLocaleString() + ' pages (' + pct + '%)';
|
||||
} else if (data.type === 'CACHE_COMPLETE') {
|
||||
cacheProgress.classList.add('active');
|
||||
progressFill.style.width = '100%';
|
||||
progressText.innerHTML = '<span class="cache-complete">✓ Download complete! ' + data.total.toLocaleString() + ' pages available offline.</span>';
|
||||
startCacheBtn.textContent = 'Downloaded!';
|
||||
startCacheBtn.disabled = true;
|
||||
// Refresh the cache list
|
||||
setTimeout(checkCaches, 500);
|
||||
} else if (data.type === 'CACHE_ERROR') {
|
||||
cacheProgress.classList.add('active');
|
||||
progressText.innerHTML = '<span class="status-error">Error: ' + data.error + '</span>';
|
||||
startCacheBtn.disabled = false;
|
||||
startCacheBtn.textContent = 'Try Again';
|
||||
}
|
||||
});
|
||||
|
||||
// Check if caching is already in progress when page loads
|
||||
navigator.serviceWorker.ready.then(function(registration) {
|
||||
if (registration.active) {
|
||||
registration.active.postMessage({ type: 'GET_CACHE_STATUS' });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Bible Reader functionality
|
||||
|
||||
Reference in New Issue
Block a user