Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/6831.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix production frontend hydration on Windows when the system MIME registry maps JavaScript files to `text/plain`.
7 changes: 5 additions & 2 deletions reflex/utils/precompressed_staticfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,18 @@ def file_response(
response_path: str | PathLike[str] = full_path
response_stat = stat_result
response_headers: dict[str, str] = {}
media_type: str | None = None
media_type = (
"text/javascript"
if Path(full_path).suffix.lower() in {".js", ".mjs"}
else guess_type(os.fspath(full_path))[0]
)

if self._encodings:
response_headers["Vary"] = "Accept-Encoding"
sidecar = self._select_sidecar(full_path, scope)
if sidecar is not None:
content_encoding, response_path, response_stat = sidecar
response_headers["Content-Encoding"] = content_encoding
media_type = guess_type(os.fspath(full_path))[0] or "text/plain"

response = FileResponse(
response_path,
Expand Down
29 changes: 29 additions & 0 deletions tests/units/utils/test_precompressed_staticfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,35 @@ async def test_precompressed_static_files_prefers_best_accept_encoding(
assert await _collect_body(response, scope) == b"compressed-brotli"


@pytest.mark.asyncio
@pytest.mark.parametrize(
("accept_encoding", "response_name"),
[("identity", "app.js"), ("gzip", "app.js.gz")],
)
async def test_precompressed_static_files_use_javascript_mime_independent_of_system_mapping(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
accept_encoding: str,
response_name: str,
):
"""Serve JavaScript with its standard MIME type despite a host override."""
(tmp_path / "app.js").write_text("console.log('hello');")
(tmp_path / "app.js.gz").write_bytes(b"compressed-gzip")
monkeypatch.setattr(
"reflex.utils.precompressed_staticfiles.guess_type",
lambda _path: ("text/plain", None),
)

static_files = PrecompressedStaticFiles(directory=tmp_path, encodings=["gzip"])
response = await static_files.get_response(
"app.js", _scope("/app.js", accept_encoding)
)

assert isinstance(response, FileResponse)
assert str(response.path).endswith(response_name)
assert response.media_type == "text/javascript"


@pytest.mark.asyncio
async def test_precompressed_static_files_fall_back_to_identity(tmp_path: Path):
"""Keep serving the original file when no accepted sidecar is available."""
Expand Down
Loading