Sync offline cache on every page load, not just every 2 minutes

Online-side mutations (e.g. deleting a tag) only update the server, not
IndexedDB, so a throttled background sync could leave the offline cache
stale for up to two minutes. Going offline in that window resurrected
deleted/stale data (e.g. a deleted tag reappearing). Removing the throttle
so every real page load pulls fresh state keeps the offline cache in
sync with whatever was just done online.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Keith Smith
2026-09-05 08:36:49 -06:00
co-authored by Claude Sonnet 5
parent bde7be454b
commit c454423d4d
4 changed files with 13 additions and 17 deletions
+10 -2
View File
@@ -3,12 +3,20 @@
* Handles theme toggle, task selection, mobile menus, and AJAX operations
*/
document.addEventListener('DOMContentLoaded', function() {
document.addEventListener('DOMContentLoaded', async function() {
initTheme();
initTaskSelection();
initTimerDisplays();
registerServiceWorker();
maybeBackgroundSync();
// Every real page load is a chance for the online UI to have mutated
// something (a tag delete, a task edit) that IndexedDB doesn't know
// about yet - sync unconditionally (no throttle) so the offline cache
// never lags behind what the user just did while online. The server's
// own rate limit (SyncRateThrottle) is the backstop against excess calls.
const pushedChanges = await runBackgroundSync();
if (pushedChanges) {
window.location.reload();
}
});
window.addEventListener('online', async function() {
-12
View File
@@ -5,8 +5,6 @@
* network-loaded page (never from the cached offline app).
*/
const BACKGROUND_SYNC_MIN_INTERVAL_MS = 2 * 60 * 1000;
// Bump this whenever a previously-untracked entity type starts being synced
// (e.g. tags/time_entries were added after tasks-only syncing already
// shipped). /api/sync/ only returns rows changed since a device's last sync
@@ -173,13 +171,3 @@ async function runBackgroundSync() {
return hadPendingChanges || forceFullResync;
}
async function maybeBackgroundSync() {
const lastSyncAt = await getMeta('last_sync_at');
if (lastSyncAt && Date.now() - new Date(lastSyncAt).getTime() < BACKGROUND_SYNC_MIN_INTERVAL_MS) {
return;
}
const pushedChanges = await runBackgroundSync();
if (pushedChanges) {
window.location.reload();
}
}
+2 -2
View File
@@ -148,8 +148,8 @@
{% endblock %}
<script src="{% static 'js/offline-db.js' %}?v=7"></script>
<script src="{% static 'js/offline-sync.js' %}?v=9"></script>
<script src="{% static 'js/app.js' %}?v=8"></script>
<script src="{% static 'js/offline-sync.js' %}?v=10"></script>
<script src="{% static 'js/app.js' %}?v=9"></script>
{% block extra_js %}{% endblock %}
</body>
</html>
+1 -1
View File
@@ -1,4 +1,4 @@
{% load static %}const CACHE_NAME = 'keepitgoing-shell-v7';
{% load static %}const CACHE_NAME = 'keepitgoing-shell-v8';
const OFFLINE_URL = '{% url "offline" %}';
const OFFLINE_TASKS_URL = '{% url "offline-tasks" %}';