From 9b069283d59e976a691e2580453a84a7481fc950 Mon Sep 17 00:00:00 2001 From: shinve <218882134+lottshin@users.noreply.github.com> Date: Tue, 27 Oct 2026 16:05:33 +0000 Subject: [PATCH] fix(windows): restore MIME correction for multi-dist frontend assets --- backend/single_port_app.py | 2 +- backend/tests/test_single_port_app.py | 50 +++++++++++++++++++-------- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/backend/single_port_app.py b/backend/single_port_app.py index 87b252c05..50b5207ae 100644 --- a/backend/single_port_app.py +++ b/backend/single_port_app.py @@ -339,7 +339,7 @@ def file_response( return response -class MultiDistStaticFiles(StaticFiles): +class MultiDistStaticFiles(FrontendStaticFiles): """Serve one asset tree that may live in either dist (the releases build or the business one), so a page rendered from one build still loads its own hashed assets while the other build is being served. diff --git a/backend/tests/test_single_port_app.py b/backend/tests/test_single_port_app.py index cfabf796a..fe2ae89a1 100644 --- a/backend/tests/test_single_port_app.py +++ b/backend/tests/test_single_port_app.py @@ -43,23 +43,45 @@ async def receive(): ) -def test_javascript_assets_override_broken_windows_mime_mapping(tmp_path: Path) -> None: - original_media_type = mimetypes.guess_type("bundle.js")[0] - mimetypes.add_type("text/plain", ".js", strict=True) +@pytest.mark.parametrize( + "prefix", + ["/assets", "/enterprise/assets", "/chat/assets", "/workspace/assets", "/admin/assets"], +) +@pytest.mark.parametrize("method", ["GET", "HEAD"]) +def test_javascript_assets_override_broken_windows_mime_mapping( + monkeypatch, + tmp_path: Path, + prefix: str, + method: str, +) -> None: + releases = tmp_path / "releases" / "assets" + business = tmp_path / "business" / "assets" + releases.mkdir(parents=True) + business.mkdir(parents=True) + payload = b"export const ready = true;" + # Only the fallback dist contains the asset referenced by an older page. + (business / "bundle.js").write_bytes(payload) + + # Initialize before patching so the Windows registry cannot replace the map. + mimetypes.guess_type("bundle.js") + monkeypatch.setitem(mimetypes.types_map, ".js", "text/plain") - try: - asset_dir = tmp_path / "assets" - asset_dir.mkdir() - (asset_dir / "bundle.js").write_text("export const ready = true;", encoding="utf-8") - app = FastAPI() - app.mount("/assets", single_port_app.FrontendStaticFiles(directory=asset_dir)) + # Exercise the registered handler, including its production MIME policy. + mount = next( + route for route in single_port_app.app.routes if getattr(route, "path", None) == prefix + ) + monkeypatch.setattr(mount.app, "directory", releases) + monkeypatch.setattr(mount.app, "all_directories", [str(releases), str(business)]) + monkeypatch.setattr(mount.app, "config_checked", False) + app = FastAPI() + app.router.routes.append(mount) - response = TestClient(app).head("/assets/bundle.js") + response = TestClient(app).request(method, f"{prefix}/bundle.js") - assert response.status_code == 200 - assert response.headers["content-type"] == "text/javascript; charset=utf-8" - finally: - mimetypes.add_type(original_media_type or "text/javascript", ".js", strict=True) + assert response.status_code == 200 + assert response.headers["content-type"] == "text/javascript; charset=utf-8" + assert response.headers["content-length"] == str(len(payload)) + assert response.content == (payload if method == "GET" else b"") def test_valid_application_javascript_mapping_is_not_reported_as_correction(