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>
This commit is contained in:
ksmithandClaude Sonnet 5 committed 2026-10-31 12:03:10 -06:00
1 parent b52ba2be8a
commit d7b433e580
2 files changed
+20 -1

No files matched your search

+5
View File
@@ -32,6 +32,11 @@ def test_service_worker_is_served_at_root_with_correct_type_and_scope(client):
assert 'self.addEventListener("fetch"' in body assert 'self.addEventListener("fetch"' in body
# Mutations must stay network-only, never intercepted. # Mutations must stay network-only, never intercepted.
assert 'request.method !== "GET"' in body assert 'request.method !== "GET"' in body
# Logging out must purge PAGES_CACHE so a later offline navigation on
# a shared/account-switched device can't serve another user's
# previously cached authenticated pages.
assert "caches.delete(PAGES_CACHE)" in body
assert reverse("accounts:logout") in body
@pytest.mark.django_db @pytest.mark.django_db
+15 -1
View File
@@ -66,8 +66,23 @@ async function cacheFirstForAsset(request) {
return response; return response;
} }
const LOGOUT_PATH = "{% url 'accounts:logout' %}";
self.addEventListener("fetch", (event) => { self.addEventListener("fetch", (event) => {
const { request } = 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 // Mutations (mark-read, star, OPML upload, forms...) are always
// network-only — never intercept anything but GET. // network-only — never intercept anything but GET.
@@ -78,7 +93,6 @@ self.addEventListener("fetch", (event) => {
return; return;
} }
const url = new URL(request.url);
if (url.pathname.startsWith("/static/")) { if (url.pathname.startsWith("/static/")) {
event.respondWith(cacheFirstForAsset(request)); event.respondWith(cacheFirstForAsset(request));
} }