perf: bundle and fingerprint the PWA runtime (#379)
CI / lint (pull_request) Successful in 42s
CI / build-release (pull_request) Successful in 5s
CI / release-candidate (pull_request) Has been skipped

This commit is contained in:
timmy committed 2026-08-09 06:59:05 +00:00
1 parent 56a9a0cba6
commit a919242b8f
8 files changed
+187 -16

No files matched your search

+10 -1
View File
@@ -1,9 +1,17 @@
import re
from pathlib import Path
from src.views import dashboard as dashboard_html
FRONTEND = Path(__file__).resolve().parents[1] / "frontend"
PAGE_SCRIPT_MANIFEST = "\n".join(
re.findall(
r'^<script src="static/[^\"]+\.js"></script>$',
(FRONTEND / "index.html").read_text(),
re.MULTILINE,
)
)
def dashboard_bundle_text() -> str:
@@ -18,11 +26,12 @@ def dashboard_bundle_text() -> str:
async def dashboard() -> str:
# Exercise the real view lookup before adding its separately served assets.
# Exercise the real view lookup before adding its separately served bootstrap assets.
html = await dashboard_html()
return "\n".join(
(
html,
PAGE_SCRIPT_MANIFEST,
(FRONTEND / "dashboard.css").read_text(),
(FRONTEND / "dashboard.js").read_text(),
)
+9 -1
View File
@@ -7,6 +7,7 @@ import pytest
from src import main
from src.session_store import SessionStoreError
from src.views import FRONTEND_BUILD
@pytest.fixture
@@ -956,9 +957,16 @@ async def test_public_routes_skip_session_registry_validation(access_control, mo
login = await client.get("/login")
manifest = await client.get("/manifest.webmanifest")
static = await client.get("/static/session.js")
runtime = await client.get("/" + FRONTEND_BUILD.runtime_name)
assert signed_in.status_code == 200
assert [login.status_code, manifest.status_code, static.status_code] == [200, 200, 200]
assert [login.status_code, manifest.status_code, static.status_code, runtime.status_code] == [
200,
200,
200,
200,
]
assert runtime.headers["cache-control"] == "public, max-age=31536000, immutable"
@pytest.mark.anyio
+62
View File
@@ -0,0 +1,62 @@
import gzip
import re
import shutil
from pathlib import Path
import pytest
from src.frontend_bundle import build_frontend
from src.views import dashboard, runtime_bundle, service_worker
FRONTEND = Path(__file__).resolve().parents[1] / "frontend"
PAGE_SCRIPT = re.compile(r'<script src="(static/[^"]+\.js)"></script>')
def test_page_runtime_is_one_deterministic_content_addressed_bundle(tmp_path):
first = build_frontend(FRONTEND)
second = build_frontend(FRONTEND)
assert first.runtime_name == second.runtime_name
assert first.runtime_bytes == second.runtime_bytes
assert PAGE_SCRIPT.findall(first.dashboard_html) == []
assert first.dashboard_html.count("<script src=") == 1
assert f'<script src="{first.runtime_name}"></script>' in first.dashboard_html
assert first.runtime_name.startswith("runtime-")
assert first.runtime_name.endswith(".js")
assert len(gzip.compress(first.runtime_bytes, mtime=0)) <= 100 * 1024
changed_frontend = tmp_path / "frontend"
shutil.copytree(FRONTEND, changed_frontend)
(changed_frontend / "widgets.js").write_text(
(changed_frontend / "widgets.js").read_text() + "\n// changed\n"
)
changed = build_frontend(changed_frontend)
assert changed.runtime_name != first.runtime_name
assert changed.runtime_bytes != first.runtime_bytes
def test_offline_shell_precaches_exact_runtime_without_superseded_page_modules():
build = build_frontend(FRONTEND)
assert f"BASE + '{build.runtime_name}'" in build.service_worker_source
for source in build.page_sources:
if source == "static/background-issue-sync.js":
continue
assert f"BASE + '{source}'" not in build.service_worker_source
@pytest.mark.anyio
async def test_runtime_asset_is_immutable_while_html_and_worker_revalidate():
build = build_frontend(FRONTEND)
html = await dashboard()
bundle = await runtime_bundle(build.runtime_digest)
worker = await service_worker()
assert build.runtime_name in html
assert bundle.body == build.runtime_bytes
assert bundle.headers["cache-control"] == "public, max-age=31536000, immutable"
assert worker.headers["cache-control"] == "no-cache"
assert build.runtime_name.encode() in worker.body
+3 -3
View File
@@ -2,7 +2,7 @@ from pathlib import Path
import pytest
from src.views import dashboard
from src.views import FRONTEND_BUILD, dashboard
@pytest.mark.anyio
@@ -28,7 +28,7 @@ async def test_dashboard_bootstrap_uses_csp_compatible_static_assets():
html = await dashboard()
assert '<link rel="stylesheet" href="static/dashboard.css" />' in html
assert '<script src="static/dashboard.js"></script>' in html
assert f'<script src="{FRONTEND_BUILD.runtime_name}"></script>' in html
assert "<style>" not in html
assert "<script>" not in html
@@ -42,7 +42,7 @@ async def test_global_search_preview_is_a_phone_safe_accessible_dialog():
assert 'aria-labelledby="search-preview-title"' in html
assert 'id="claim-search-result"' in html
assert 'id="open-search-result-gitea"' in html
assert 'src="static/search-preview.js"' in html
assert "static/search-preview.js" in FRONTEND_BUILD.page_sources
assert ".search-preview-panel" in css
assert "height:100dvh" in css
assert "env(safe-area-inset-bottom)" in css