Files
Evection/templates/pwa/sw.js
T
ksmithandClaude Sonnet 5 d7b433e580 Purge the service worker's authenticated-page cache on logout (S3)
PAGES_CACHE held every cached navigation response (including
authenticated article pages) with no logout-time purge, so a later
offline navigation on a shared or account-switched device could still
serve another user's cached pages. Logout is a plain POST, so the
fetch handler now special-cases that request: it deletes PAGES_CACHE
and lets the real logout request through, with no race against the
navigation that follows since it's synchronous with the same request.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-31 12:03:10 -06:00

100 lines
3.2 KiB
JavaScript

{% load static %}const CACHE_VERSION = "{{ cache_version }}";
const SHELL_CACHE = `evection-shell-${CACHE_VERSION}`;
const PAGES_CACHE = `evection-pages-${CACHE_VERSION}`;
const MAX_CACHED_PAGES = 60;
const SHELL_URLS = [
"{% url 'pwa:offline' %}",
"{% url 'accounts:login' %}",
"{% static 'css/base.css' %}",
"{% static 'js/htmx.min.js' %}",
"{% static 'pwa/icons/icon-192.png' %}",
"{% static 'pwa/icons/icon-512.png' %}",
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(SHELL_CACHE).then((cache) => cache.addAll(SHELL_URLS)).then(() => self.skipWaiting())
);
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches
.keys()
.then((names) =>
Promise.all(
names
.filter((name) => name !== SHELL_CACHE && name !== PAGES_CACHE)
.map((name) => caches.delete(name))
)
)
.then(() => self.clients.claim())
);
});
async function trimCache(cacheName, maxItems) {
const cache = await caches.open(cacheName);
const keys = await cache.keys();
if (keys.length <= maxItems) return;
// Cache.keys() returns insertion order, so the oldest entries are first.
const toDelete = keys.slice(0, keys.length - maxItems);
await Promise.all(toDelete.map((key) => cache.delete(key)));
}
async function networkFirstForNavigation(request) {
try {
const response = await fetch(request);
const cache = await caches.open(PAGES_CACHE);
cache.put(request, response.clone());
trimCache(PAGES_CACHE, MAX_CACHED_PAGES);
return response;
} catch (err) {
const cached = await caches.match(request);
if (cached) return cached;
const offline = await caches.match("{% url 'pwa:offline' %}");
return offline || Response.error();
}
}
async function cacheFirstForAsset(request) {
const cached = await caches.match(request);
if (cached) return cached;
const response = await fetch(request);
const cache = await caches.open(SHELL_CACHE);
cache.put(request, response.clone());
return response;
}
const LOGOUT_PATH = "{% url 'accounts:logout' %}";
self.addEventListener("fetch", (event) => {
const { request } = event;
const url = new URL(request.url);
// Logging out ends this device's session, so any authenticated HTML
// sitting in PAGES_CACHE must not outlive it — otherwise a later
// offline navigation (shared device, different account logging in
// next) could still serve a previous user's cached article pages.
// Purging it synchronously with the logout request itself, rather
// than via a postMessage from the page, means there's no race with
// the navigation that follows.
if (request.method === "POST" && url.pathname === LOGOUT_PATH) {
event.respondWith(caches.delete(PAGES_CACHE).then(() => fetch(request)));
return;
}
// Mutations (mark-read, star, OPML upload, forms...) are always
// network-only — never intercept anything but GET.
if (request.method !== "GET") return;
if (request.mode === "navigate") {
event.respondWith(networkFirstForNavigation(request));
return;
}
if (url.pathname.startsWith("/static/")) {
event.respondWith(cacheFirstForAsset(request));
}
});