diff --git a/backend/app/main.py b/backend/app/main.py index 58d492d..e8cffbb 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -74,6 +74,20 @@ async def lifespan(_app: FastAPI) -> AsyncIterator[None]: task.cancel() +# Hashed build output never changes; everything else must be revalidated so updates arrive +_IMMUTABLE = "public, max-age=31536000, immutable" +_REVALIDATE = "no-cache" +_MEDIA_TYPES = {".webmanifest": "application/manifest+json"} + + +def _cache_control(relative: str) -> str: + if relative.startswith("assets/") or relative.startswith("workbox-"): + return _IMMUTABLE + if relative.startswith("icons/"): + return "public, max-age=86400" + return _REVALIDATE # index.html, sw.js, manifest.webmanifest + + def _register_frontend(app: FastAPI, static_dir: Path) -> None: """Serve the built Vue SPA; unknown non-API paths fall back to index.html.""" root = static_dir.resolve() @@ -85,12 +99,16 @@ def _register_frontend(app: FastAPI, static_dir: Path) -> None: raise HTTPException(status_code=404) candidate = (root / path).resolve() if path and candidate.is_relative_to(root) and candidate.is_file(): - return FileResponse(candidate) + return FileResponse( + candidate, + media_type=_MEDIA_TYPES.get(candidate.suffix), + headers={"Cache-Control": _cache_control(path)}, + ) # Paths that look like files (/.env, /wp-login.php, /.git/config) are not app routes segments = path.split("/") if "." in segments[-1] or any(seg.startswith(".") for seg in segments): raise HTTPException(status_code=404) - return FileResponse(index_file, headers={"Cache-Control": "no-cache"}) + return FileResponse(index_file, headers={"Cache-Control": _REVALIDATE}) def create_app() -> FastAPI: diff --git a/backend/tests/test_pwa.py b/backend/tests/test_pwa.py new file mode 100644 index 0000000..af1e630 --- /dev/null +++ b/backend/tests/test_pwa.py @@ -0,0 +1,39 @@ +import pytest + + +@pytest.fixture +def built(static_dir): + """Minimal stand-in for the frontend build output.""" + (static_dir / "assets").mkdir() + (static_dir / "icons").mkdir() + (static_dir / "index.html").write_text("spa") + (static_dir / "sw.js").write_text("self.addEventListener('fetch', () => {})") + (static_dir / "workbox-abc123.js").write_text("// workbox") + (static_dir / "manifest.webmanifest").write_text('{"name": "LAN-Clipboard"}') + (static_dir / "assets" / "index-abc123.js").write_text("console.log(1)") + (static_dir / "icons" / "icon-192.png").write_bytes(b"\\x89PNG") + + +@pytest.mark.parametrize("path", ["/", "/clips/1", "/index.html", "/sw.js"]) +def test_entry_points_are_revalidated(built, client, path): + assert client.get(path).headers["cache-control"] == "no-cache" + + +@pytest.mark.parametrize("path", ["/assets/index-abc123.js", "/workbox-abc123.js"]) +def test_hashed_files_are_immutable(built, client, path): + assert client.get(path).headers["cache-control"] == "public, max-age=31536000, immutable" + + +def test_icons_are_cached_for_a_day(built, client): + assert client.get("/icons/icon-192.png").headers["cache-control"] == "public, max-age=86400" + + +def test_manifest_content_type(built, client): + response = client.get("/manifest.webmanifest") + assert response.status_code == 200 + assert response.headers["content-type"].startswith("application/manifest+json") + assert response.headers["cache-control"] == "no-cache" + + +def test_service_worker_is_javascript(built, client): + assert "javascript" in client.get("/sw.js").headers["content-type"]