diora-web/static/js/sw.js

108 lines
3.3 KiB
JavaScript
Raw Normal View History

2026-03-16 19:19:22 +01:00
/**
* diora service worker caches the app shell for offline use.
*/
Reader: Marker-Modus statt impliziter Auswahl, Notiz-Anker rein positional (SW v26) Vier vom Nutzer gemeldete Bugs behoben: - Markierter Text war kaum lesbar: der globale weiße Text-Outline (fürs Hintergrundbild-Feature gedacht) kollidierte mit der Highlight-Farbe. Reader sitzt immer auf eigenem, opakem Hintergrund — Outline dort komplett deaktiviert. - "Notiz zu Markierung hinzufügen" öffnete weiterhin die rechte Sidebar statt den Rand. Jeder Notiz-Eintragspunkt (Highlight+Notiz aus der Textauswahl, Edit-Note-Klick auf eine Markierung im Fließtext) läuft jetzt über _openMarginAndEditNote() — öffnet bei Bedarf den Rand und bearbeitet dort inline. openNoteEditor()/rechte Sidebar für Notizen komplett entfernt. - Kernursache für "Notiz verschwindet, Markierung heftet sich an falsche Notiz": Anker basierten auf XPath mit Sibling-Index unter den Textknoten eines Blocks — jede weitere Markierung im selben Block (surroundContents spaltet Textknoten) verschob diese Indizes und korrumpierte lautlos ALLE anderen Anker im selben Block. Anker adressieren jetzt stattdessen Block-Index + Zeichen-Offset innerhalb des Blocks (wie das bestehende Lesefortschritt-Ankersystem) — Wrapping/Unwrapping durch andere Markierungen ändert weder Blockreihenfolge noch Zeichenanzahl, ist also erschütterungsfrei für andere Anker im selben Block. - Löschen einer Markierung riss zuvor aus demselben Grund weiter unten liegende Notizen optisch mit raus (Daten blieben in der DB) — mit dem neuen Ankersystem behoben. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-04 13:28:19 +02:00
const CACHE = 'diora-v26';
const PODCAST_CACHE = 'diora-podcast-v1';
2026-03-16 19:19:22 +01:00
const SHELL = [
'/static/css/app.css',
'/static/js/app.js',
'/static/manifest.json',
];
// Install: pre-cache the app shell
self.addEventListener('install', function (event) {
event.waitUntil(
caches.open(CACHE).then(function (cache) {
return cache.addAll(SHELL);
})
);
// Activate immediately without waiting for old tabs to close
self.skipWaiting();
});
// Activate: remove stale caches from previous versions
self.addEventListener('activate', function (event) {
event.waitUntil(
caches.keys().then(function (keys) {
return Promise.all(
keys.filter(function (key) { return key !== CACHE && key !== PODCAST_CACHE; })
2026-03-16 19:19:22 +01:00
.map(function (key) { return caches.delete(key); })
);
}).then(function () {
return self.clients.claim();
}).then(function () {
// Tell all open tabs to reload so they get the latest cached assets
return self.clients.matchAll({type: 'window'}).then(function (clients) {
clients.forEach(function (client) {
client.postMessage({type: 'SW_ACTIVATED'});
});
});
2026-03-16 19:19:22 +01:00
})
);
});
// Fetch: serve from cache, fall back to network
self.addEventListener('fetch', function (event) {
// Only handle GET requests; let POST/SSE etc. pass through
if (event.request.method !== 'GET') return;
const url = new URL(event.request.url);
// Bypass for API/mutation endpoints
2026-03-16 19:19:22 +01:00
if (url.pathname.startsWith('/radio/sse/') ||
url.pathname.startsWith('/radio/record/') ||
url.pathname.startsWith('/radio/affiliate/') ||
url.pathname.startsWith('/admin/') ||
url.pathname.startsWith('/podcasts/progress/') ||
url.pathname.startsWith('/podcasts/queue/') ||
url.pathname === '/podcasts/feeds/add' ||
url.pathname.startsWith('/podcasts/feeds/add') ||
url.pathname.includes('/remove') ||
url.pathname.startsWith('/podcasts/feeds/refresh')) {
return;
}
// Podcast audio: serve from podcast cache first, then network
if (event.request.destination === 'audio') {
event.respondWith(
caches.open(PODCAST_CACHE).then(function (cache) {
return cache.match(event.request).then(function (cached) {
if (cached) return cached;
return fetch(event.request).then(function (response) {
return response;
});
});
})
);
2026-03-16 19:19:22 +01:00
return;
}
// Cache-first for pre-defined shell assets
const isShell = SHELL.some(function (s) { return url.pathname === s; });
if (isShell) {
event.respondWith(
caches.match(event.request).then(function (cached) {
return cached || fetch(event.request);
})
);
return;
}
// Navigation requests (the HTML page itself): network-first, fall back to
// last cached version so the app opens offline after being visited once.
if (event.request.mode === 'navigate') {
event.respondWith(
fetch(event.request).then(function (response) {
if (response.ok) {
var clone = response.clone();
caches.open(CACHE).then(function (cache) { cache.put(event.request, clone); });
}
return response;
}).catch(function () {
return caches.match(event.request);
})
);
}
2026-03-16 19:19:22 +01:00
});