github-actions[bot] commited on
Commit ·
f3c2347
1
Parent(s): eb9318e
Automated Deploy: 4b01430ffe936d9f36ef0be206e9a47a3c53010d
Browse files- backend/app/main.py +8 -2
- backend/tests/test_frontend.py +39 -0
- frontend/dist/index.html +1 -1
- scripts/verify_frontend.py +38 -0
backend/app/main.py
CHANGED
|
@@ -45,10 +45,16 @@ def frontend_app(path: str):
|
|
| 45 |
raise HTTPException(404, "Route not found")
|
| 46 |
candidate = (frontend / path).resolve()
|
| 47 |
if candidate.is_relative_to(frontend.resolve()) and candidate.is_file():
|
| 48 |
-
return FileResponse(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
index = frontend / "index.html"
|
| 50 |
if not index.is_file():
|
| 51 |
raise HTTPException(
|
| 52 |
404, "Build the frontend or start the Vite development server"
|
| 53 |
)
|
| 54 |
-
return FileResponse(index)
|
|
|
|
| 45 |
raise HTTPException(404, "Route not found")
|
| 46 |
candidate = (frontend / path).resolve()
|
| 47 |
if candidate.is_relative_to(frontend.resolve()) and candidate.is_file():
|
| 48 |
+
return FileResponse(
|
| 49 |
+
candidate,
|
| 50 |
+
headers={"Cache-Control": "public, max-age=31536000, immutable"}
|
| 51 |
+
if path.startswith("assets/") else {"Cache-Control": "no-cache"},
|
| 52 |
+
)
|
| 53 |
+
if path.startswith("assets/") or Path(path).suffix:
|
| 54 |
+
raise HTTPException(404, "Frontend asset not found")
|
| 55 |
index = frontend / "index.html"
|
| 56 |
if not index.is_file():
|
| 57 |
raise HTTPException(
|
| 58 |
404, "Build the frontend or start the Vite development server"
|
| 59 |
)
|
| 60 |
+
return FileResponse(index, headers={"Cache-Control": "no-cache"})
|
backend/tests/test_frontend.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import importlib
|
| 2 |
+
import importlib.util
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import pytest
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def test_static_assets_and_spa_fallback(client, monkeypatch, tmp_path):
|
| 8 |
+
main = importlib.import_module("app.main")
|
| 9 |
+
monkeypatch.setattr(main, "frontend", tmp_path)
|
| 10 |
+
(tmp_path / "index.html").write_text('<script type="module" src="/assets/app.js"></script>')
|
| 11 |
+
(tmp_path / "assets").mkdir()
|
| 12 |
+
(tmp_path / "assets/app.js").write_text('console.log("loaded")')
|
| 13 |
+
for route in ("/", "/renders"):
|
| 14 |
+
response = client.get(route)
|
| 15 |
+
assert response.status_code == 200
|
| 16 |
+
assert response.headers["cache-control"] == "no-cache"
|
| 17 |
+
assert response.headers["content-type"].startswith("text/html")
|
| 18 |
+
script = client.get("/assets/app.js")
|
| 19 |
+
assert script.status_code == 200
|
| 20 |
+
assert "javascript" in script.headers["content-type"]
|
| 21 |
+
assert "immutable" in script.headers["cache-control"]
|
| 22 |
+
for route in ("/assets/missing.js", "/missing.css", "/api/v1/missing"):
|
| 23 |
+
response = client.get(route)
|
| 24 |
+
assert response.status_code == 404
|
| 25 |
+
assert "text/html" not in response.headers["content-type"]
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def test_deploy_rejects_stale_html(tmp_path):
|
| 29 |
+
path = Path(__file__).resolve().parents[2] / "scripts/verify_frontend.py"
|
| 30 |
+
spec = importlib.util.spec_from_file_location("verify_frontend", path)
|
| 31 |
+
module = importlib.util.module_from_spec(spec)
|
| 32 |
+
spec.loader.exec_module(module)
|
| 33 |
+
(tmp_path / "index.html").write_text('<script type="module" src="/assets/old.js"></script>')
|
| 34 |
+
(tmp_path / "assets").mkdir()
|
| 35 |
+
(tmp_path / "assets/new.js").write_text("export default 1")
|
| 36 |
+
with pytest.raises(ValueError, match="old.js"):
|
| 37 |
+
module.verify(tmp_path)
|
| 38 |
+
(tmp_path / "index.html").write_text('<script type="module" src="/assets/new.js"></script>')
|
| 39 |
+
module.verify(tmp_path)
|
frontend/dist/index.html
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
<!doctype html><html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta name="theme-color" content="#17191d"/><meta name="description" content="Create original characters. Direct their next conversation."/><title>Character Studio</title> <script type="module" crossorigin src="/assets/index-
|
| 2 |
<link rel="stylesheet" crossorigin href="/assets/index-DESwSF5S.css">
|
| 3 |
</head><body><div id="root"></div></body></html>
|
|
|
|
| 1 |
+
<!doctype html><html lang="en"><head><meta charset="UTF-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta name="theme-color" content="#17191d"/><meta name="description" content="Create original characters. Direct their next conversation."/><title>Character Studio</title> <script type="module" crossorigin src="/assets/index-Bfi-8Lau.js"></script>
|
| 2 |
<link rel="stylesheet" crossorigin href="/assets/index-DESwSF5S.css">
|
| 3 |
</head><body><div id="root"></div></body></html>
|
scripts/verify_frontend.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Reject deployments whose HTML references missing JS or CSS bundles."""
|
| 2 |
+
from html.parser import HTMLParser
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
import sys
|
| 5 |
+
from urllib.parse import unquote, urlsplit
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class BundleReferences(HTMLParser):
|
| 9 |
+
def __init__(self):
|
| 10 |
+
super().__init__()
|
| 11 |
+
self.references = []
|
| 12 |
+
|
| 13 |
+
def handle_starttag(self, tag, attrs):
|
| 14 |
+
attrs = dict(attrs)
|
| 15 |
+
if tag == "script" and attrs.get("src"):
|
| 16 |
+
self.references.append(attrs["src"])
|
| 17 |
+
if tag == "link" and attrs.get("rel") in {"stylesheet", "modulepreload"}:
|
| 18 |
+
self.references.append(attrs.get("href", ""))
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def verify(folder):
|
| 22 |
+
folder = Path(folder).resolve()
|
| 23 |
+
parser = BundleReferences()
|
| 24 |
+
parser.feed((folder / "index.html").read_text())
|
| 25 |
+
if not parser.references:
|
| 26 |
+
raise ValueError("Frontend HTML does not reference any bundles")
|
| 27 |
+
for reference in parser.references:
|
| 28 |
+
url = urlsplit(reference)
|
| 29 |
+
if url.scheme or url.netloc:
|
| 30 |
+
continue
|
| 31 |
+
asset = (folder / unquote(url.path).lstrip("/")).resolve()
|
| 32 |
+
if not asset.is_relative_to(folder) or not asset.is_file():
|
| 33 |
+
raise ValueError(f"Frontend bundle is missing: {reference}")
|
| 34 |
+
print("Frontend bundle references verified")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
if __name__ == "__main__":
|
| 38 |
+
verify(sys.argv[1])
|