diff --git a/clips/templates/clips/index.html b/clips/templates/clips/index.html index 9d87f04..a2d79b5 100644 --- a/clips/templates/clips/index.html +++ b/clips/templates/clips/index.html @@ -21,11 +21,28 @@ and PUTs with ordinary CSRF protection — no endpoint in this app is exempt. arthur + + + + + + {% csrf_token %}
+ diff --git a/clips/tests/test_api.py b/clips/tests/test_api.py index 5d2aa52..84fed0b 100644 --- a/clips/tests/test_api.py +++ b/clips/tests/test_api.py @@ -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/ — 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 diff --git a/clips/views.py b/clips/views.py index 1f6306e..ba12b18 100644 --- a/clips/views.py +++ b/clips/views.py @@ -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/`, 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 # diff --git a/server/urls.py b/server/urls.py index 6c42133..1ff70e4 100644 --- a/server/urls.py +++ b/server/urls.py @@ -14,6 +14,9 @@ urlpatterns = [ # same page; the client reads the id out of the path and opens it. path("p/", views.page), path("p//", views.page), + # The service worker, at the root so it can control every page; see + # `views.service_worker`. + path("sw.js", views.service_worker), path("api/", include("clips.urls")), path("blob/", views.blob, name="blob"), path("admin/", admin.site.urls), diff --git a/static/arthur/icons/icon-180.png b/static/arthur/icons/icon-180.png new file mode 100644 index 0000000..25c6367 Binary files /dev/null and b/static/arthur/icons/icon-180.png differ diff --git a/static/arthur/icons/icon-192.png b/static/arthur/icons/icon-192.png new file mode 100644 index 0000000..08526cd Binary files /dev/null and b/static/arthur/icons/icon-192.png differ diff --git a/static/arthur/icons/icon-512.png b/static/arthur/icons/icon-512.png new file mode 100644 index 0000000..4801980 Binary files /dev/null and b/static/arthur/icons/icon-512.png differ diff --git a/static/arthur/manifest.webmanifest b/static/arthur/manifest.webmanifest new file mode 100644 index 0000000..3480d22 --- /dev/null +++ b/static/arthur/manifest.webmanifest @@ -0,0 +1,15 @@ +{ + "name": "arthur", + "short_name": "arthur", + "description": "Draw and animate, frame by frame.", + "start_url": "/", + "scope": "/", + "display": "standalone", + "orientation": "any", + "background_color": "#8e8e8e", + "theme_color": "#e4e4e1", + "icons": [ + {"src": "/static/arthur/icons/icon-192.png", "sizes": "192x192", "type": "image/png", "purpose": "any maskable"}, + {"src": "/static/arthur/icons/icon-512.png", "sizes": "512x512", "type": "image/png", "purpose": "any maskable"} + ] +} diff --git a/static/arthur/sw.js b/static/arthur/sw.js new file mode 100644 index 0000000..b9b149f --- /dev/null +++ b/static/arthur/sw.js @@ -0,0 +1,109 @@ +// The service worker: what arthur keeps so it opens, and shows what it has +// already shown, without the network. Served from `/sw.js` by `clips.views` +// so its scope is the whole site; see `server/urls.py`. +// +// ONE RULE PER KIND OF URL, chosen by whether what is at it can change: +// +// - CONTENT-ADDRESSED — `/blob/`, `/api/blocks/` — is named by its +// own hash, so a copy cannot be stale: cache first, keep for good. These are +// a project's frames, footage and drawings, cached as they load. +// - VERSIONED — the bundle and the stylesheet, `?v=` — is a new URL +// for every build: cache first, and drop the versions it replaces. +// - EVERYTHING ELSE of ours — the page, `/api/` reads, unversioned statics — +// can change under the same URL: the network first, the last copy offline. +// +// A Range request is left to the browser: a `