Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: /public path assets for windows (pt 2) (#3792)

Myles Scolnick26b0ce00a11f
diff --git a/marimo/_server/api/endpoints/assets.py b/marimo/_server/api/endpoints/assets.py
index f53f18c6db..8367d4342d 100644
--- a/marimo/_server/api/endpoints/assets.py
+++ b/marimo/_server/api/endpoints/assets.py
@@ -13,7 +13,7 @@ from starlette.staticfiles import StaticFiles
from marimo import _loggers
from marimo._config.manager import get_default_config_manager
-from marimo._output.utils import uri_decode_component
+from marimo._output.utils import uri_decode_component, uri_encode_component
from marimo._runtime.virtual_file import EMPTY_VIRTUAL_FILE, read_virtual_file
from marimo._server.api.deps import AppState
from marimo._server.router import APIRouter
@@ -112,14 +112,20 @@ async def index(request: Request) -> HTMLResponse:
)
# Inject service worker registration with the notebook ID
- html = inject_script(
- html,
- # Register service worker with the notebook ID
- # Potentially update the service worker and send the notebook ID again.
- f"""
+ html = _inject_service_worker(html, file_key)
+
+ return HTMLResponse(html)
+
+
+def _inject_service_worker(html: str, file_key: str) -> str:
+ return inject_script(
+ html,
+ # Register service worker with the notebook ID
+ # Potentially update the service worker and send the notebook ID again.
+ f"""
if ('serviceWorker' in navigator) {{
- const notebookId = '{file_key}';
- navigator.serviceWorker.register('./public-files-sw.js')
+ const notebookId = '{uri_encode_component(file_key)}';
+ navigator.serviceWorker.register('./public-files-sw.js?v=2')
.then(registration => {{
registration.active.postMessage({{ notebookId }});
}})
@@ -135,9 +141,7 @@ async def index(request: Request) -> HTMLResponse:
}});
}}
""",
- )
-
- return HTMLResponse(html)
+ )
STATIC_FILES = [
@@ -226,7 +230,7 @@ async def public_files_service_worker(request: Request) -> Response:
notebookIdPromise.then(notebookId => {
return fetch(event.request.url, {
headers: {
- 'X-Notebook-Id': encodeURIComponent(notebookId)
+ 'X-Notebook-Id': notebookId
}
});
})
diff --git a/tests/_server/api/endpoints/test_assets.py b/tests/_server/api/endpoints/test_assets.py
index f14f34f1dc..e9a93fdfc8 100644
--- a/tests/_server/api/endpoints/test_assets.py
+++ b/tests/_server/api/endpoints/test_assets.py
@@ -8,6 +8,7 @@ from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING, Any, cast
from marimo._server.api.deps import AppState
+from marimo._server.api.endpoints.assets import _inject_service_worker
from marimo._server.api.utils import parse_title
from marimo._server.file_router import AppFileRouter
from tests._server.mocks import token_header, with_file_router
@@ -205,3 +206,14 @@ def test_public_file_security(client: TestClient) -> None:
# Cleanup
shutil.rmtree(public_dir, ignore_errors=True)
shutil.rmtree(secret_dir, ignore_errors=True)
+
+
+def test_inject_service_worker() -> None:
+ assert (
+ "const notebookId = 'path%2Fto%2Fnotebook.py';"
+ in _inject_service_worker("<body></body>", "path/to/notebook.py")
+ )
+ assert (
+ "const notebookId = 'c%3A%5Cpath%5Cto%5Cnotebook.py';"
+ in _inject_service_worker("<body></body>", r"c:\path\to\notebook.py")
+ )