From 8e3ce87932345767b096b019d38e8d5edd6a8473 Mon Sep 17 00:00:00 2001 From: Maarten Breddels Date: Fri, 28 Jun 2024 23:11:24 +0200 Subject: [PATCH] fix: support static files on pyodide / py.cafe under a prefix (#1486) --- shiny/http_staticfiles.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/shiny/http_staticfiles.py b/shiny/http_staticfiles.py index db38f543c..9caed7563 100644 --- a/shiny/http_staticfiles.py +++ b/shiny/http_staticfiles.py @@ -11,6 +11,8 @@ from __future__ import annotations +import re + __all__ = ( "StaticFiles", "FileResponse", @@ -53,7 +55,9 @@ def __init__(self, *, directory: str | os.PathLike[str]): async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: if scope["type"] != "http": raise AssertionError("StaticFiles can't handle non-http request") - path = scope["path"] + # following starlette >=0.33, tested to be compatible with 0.32-0.37.2 + root_path = scope.get("route_root_path", scope.get("root_path", "")) + path = scope.get("route_path", re.sub(r"^" + root_path, "", scope["path"])) path_segments = path.split("/") final_path, trailing_slash = _traverse_url_path(self.dir, path_segments) if final_path is None: