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: route catch all (#2915)

Myles Scolnick4684421c817c
diff --git a/frontend/src/utils/__tests__/routes.test.ts b/frontend/src/utils/__tests__/routes.test.ts
index 56ba4fc8ab..3bfd220e98 100644
--- a/frontend/src/utils/__tests__/routes.test.ts
+++ b/frontend/src/utils/__tests__/routes.test.ts
@@ -1,6 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { describe, it, expect } from "vitest";
import { TinyRouter } from "../routes";
+import type { ParamData, Match } from "path-to-regexp";
describe("TinyRouter.match", () => {
it("should return the correct match and template for a given location", () => {
@@ -43,4 +44,90 @@ describe("TinyRouter.match", () => {
"/path1/nested",
]);
});
+
+ it("should match catch all route", () => {
+ const catchAll = "{/*path}"; // this matches CATCH_ALL in routes.py
+ const templates = ["/path1", catchAll];
+ const router = new TinyRouter(templates);
+
+ let location = { hash: "", pathname: "/anything" } as Location;
+ expect(router.match(location)).toEqual([expect.any(Object), "{/*path}"]);
+
+ location = { hash: "", pathname: "/anything/else" } as Location;
+ expect(router.match(location)).toEqual([expect.any(Object), "{/*path}"]);
+
+ location = { hash: "", pathname: "/" } as Location;
+ expect(router.match(location)).toEqual([expect.any(Object), "{/*path}"]);
+
+ location = { hash: "", pathname: "/path1" } as Location;
+ expect(router.match(location)).toEqual([expect.any(Object), "/path1"]);
+ });
+
+ it("should match routes with parameters", () => {
+ const templates = ["/users/:id", "/posts/:postId/comments"];
+ const router = new TinyRouter(templates);
+
+ const location = { hash: "", pathname: "/users/123" } as Location;
+ const [match] = router.match(location) as [Match<ParamData>, string];
+
+ expect(match).toMatchInlineSnapshot(`
+ {
+ "params": {
+ "id": "123",
+ },
+ "path": "/users/123",
+ }
+ `);
+ });
+
+ it("should match routes with multiple parameters", () => {
+ const templates = ["/users/:userId/posts/:postId"];
+ const router = new TinyRouter(templates);
+
+ const location = { hash: "", pathname: "/users/123/posts/456" } as Location;
+ const [match] = router.match(location) as [Match<ParamData>, string];
+
+ expect(match).toMatchInlineSnapshot(`
+ {
+ "params": {
+ "postId": "456",
+ "userId": "123",
+ },
+ "path": "/users/123/posts/456",
+ }
+ `);
+ });
+
+ it("should handle query parameters", () => {
+ const templates = ["/search"];
+ const router = new TinyRouter(templates);
+
+ const location = {
+ hash: "",
+ pathname: "/search",
+ search: "?q=test&page=2",
+ } as Location;
+
+ const [match] = router.match(location) as [Match<ParamData>, string];
+ expect(match).toBeDefined();
+ });
+
+ it("should match exact paths with trailing slashes", () => {
+ const templates = ["/path1", "/path2/"];
+ const router = new TinyRouter(templates);
+
+ const location1 = { hash: "", pathname: "/path1/" } as Location;
+ const location2 = { hash: "", pathname: "/path2" } as Location;
+
+ expect(router.match(location1)).toMatchInlineSnapshot(`
+ [
+ {
+ "params": {},
+ "path": "/path1/",
+ },
+ "/path1",
+ ]
+ `);
+ expect(router.match(location2)).toBe(false);
+ });
});
diff --git a/marimo/_plugins/stateless/routes.py b/marimo/_plugins/stateless/routes.py
index 633fae7ab0..11243d9ea8 100644
--- a/marimo/_plugins/stateless/routes.py
+++ b/marimo/_plugins/stateless/routes.py
@@ -54,7 +54,7 @@ class routes(UIElement[str, str]):
_name: Final[str] = "marimo-routes"
- CATCH_ALL = "/(.*)"
+ CATCH_ALL = "{/*path}"
DEFAULT = "/"
def __init__(
diff --git a/tests/_plugins/stateless/test_routes.py b/tests/_plugins/stateless/test_routes.py
index 6973ef6de2..a2d9d50e89 100644
--- a/tests/_plugins/stateless/test_routes.py
+++ b/tests/_plugins/stateless/test_routes.py
@@ -77,4 +77,6 @@ async def test_routes_non_lazy(k: Kernel, exec_req: ExecReqProvider) -> None:
routes = k.globals["routes"]
children = "<span>42</span>" "<span>45</span>"
assert children in routes.text
- assert "data-routes='[&quot;#/&quot;, &quot;/(.*)&quot;]'" in routes.text
+ assert (
+ "data-routes='[&quot;#/&quot;, &quot;{/*path}&quot;]'" in routes.text
+ )