arthur installs as an app, and keeps what projects load

A manifest and icons make it installable. A service worker at /sw.js,
so its scope is every page, keeps by kind of URL: content-addressed
blobs and blocks for good, as projects load them; the versioned bundle
and styles until the next build; and the last copy of the page, the API
reads and other statics, for when there is no network. A project opened
once opens again offline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011FLuCjp9kNH6ovwJVXSbq9
This commit is contained in:
Olive Vaughn 2026-10-11 00:42:53 -04:00
parent bbe20b3abf
commit f0af8d392a
9 changed files with 172 additions and 1 deletions

View file

@ -21,11 +21,28 @@ and PUTs with ordinary CSRF protection — no endpoint in this app is exempt.
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>arthur</title>
<link rel="stylesheet" href="{% static 'arthur/app.css' %}?v={{ css_version }}">
<link rel="manifest" href="{% static 'arthur/manifest.webmanifest' %}">
<meta name="theme-color" content="#e4e4e1">
<link rel="icon" type="image/png" href="{% static 'arthur/icons/icon-192.png' %}">
<link rel="apple-touch-icon" href="{% static 'arthur/icons/icon-180.png' %}">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-title" content="arthur">
</head>
<body>
{% csrf_token %}
<div id="app"></div>
<script src="{% static 'mediapipe/vision_bundle.js' %}"></script>
<script src="{% static 'arthur/js/main.js' %}?v={{ js_version }}"></script>
<script>
// An installable app that opens offline, and keeps what projects load —
// see `static/arthur/sw.js`.
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js');
navigator.serviceWorker.ready.then(reg => reg.active.postMessage({
keep: [...document.querySelectorAll('link[rel=stylesheet], script[src]')]
.map(el => el.href || el.src)
}));
}
</script>
</body>
</html>

View file

@ -760,6 +760,17 @@ class PageTests(TestCase):
# save path reads to write the X-CSRFToken header.
self.assertIn("csrfmiddlewaretoken", body)
def test_the_service_worker_is_served_from_the_root(self):
# At the root so its scope is every page — a project is /p/<id> — and
# never cached, or a browser would keep an old worker for good.
response = self.client.get("/sw.js")
self.assertEqual(200, response.status_code)
self.assertEqual("application/javascript", response["Content-Type"])
self.assertEqual("no-cache", response["Cache-Control"])
body = self.client.get("/").content.decode()
self.assertIn("manifest.webmanifest", body)
self.assertIn("serviceWorker.register('/sw.js')", body)
def test_the_detector_reports_a_version_derived_from_the_model(self):
# The version is the package version plus the model asset's own hash,
# because a version string in the client is one somebody has to remember to

View file

@ -37,7 +37,7 @@ from django.contrib.auth import login as auth_login, logout as auth_logout
from django.core.exceptions import ValidationError
from django.db import transaction
from django.db.models import Q
from django.http import FileResponse, HttpResponse, JsonResponse
from django.http import FileResponse, Http404, HttpResponse, JsonResponse
from django.shortcuts import render
from django.views.decorators.http import require_http_methods
@ -146,6 +146,22 @@ def page(request, project_id=None, slug=None):
})
def service_worker(request):
"""`static/arthur/sw.js`, at the root of the site.
A service worker controls only the paths under where it is served from, and
a project is `/p/<id>`, so `/static/arthur/sw.js` would control nothing that
matters. Never cached itself: a browser compares this file to decide whether
there is a new worker, so a stale copy would keep the old one for good."""
for root in settings.STATICFILES_DIRS:
path = Path(root) / "arthur" / "sw.js"
if path.exists():
response = HttpResponse(path.read_bytes(), content_type="application/javascript")
response["Cache-Control"] = "no-cache"
return response
raise Http404("no service worker")
# ---------------------------------------------------------------------------
# the detector
#