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/+route-case-insensitive.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Match page routes case-insensitively on the backend, as React Router does on the frontend, so `on_load` events and `router.page.path` resolve for a URL like `/About` instead of falling back to the 404 page.
3 changes: 2 additions & 1 deletion reflex/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ def get_route_regex(keyworded_route: str) -> re.Pattern:
# Join the regex parts and compile the regex
regex_pattern = "".join(regex_parts)
regex_pattern = f"^{regex_pattern}/?$"
return re.compile(regex_pattern)
# React Router matches route paths case-insensitively by default.
return re.compile(regex_pattern, re.IGNORECASE)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: re.IGNORECASE enables Unicode case folding, so an ASCII route such as /i also matches /İ and /ı (and /s matches /ſ). Compile with ASCII-restricted case folding to avoid routing Unicode URLs that React Router treats as unmatched.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At reflex/route.py, line 184:

<comment>`re.IGNORECASE` enables Unicode case folding, so an ASCII route such as `/i` also matches `/İ` and `/ı` (and `/s` matches `/ſ`). Compile with ASCII-restricted case folding to avoid routing Unicode URLs that React Router treats as unmatched.</comment>

<file context>
@@ -180,7 +180,8 @@ def get_route_regex(keyworded_route: str) -> re.Pattern:
     regex_pattern = f"^{regex_pattern}/?$"
-    return re.compile(regex_pattern)
+    # React Router matches route paths case-insensitively by default.
+    return re.compile(regex_pattern, re.IGNORECASE)
 
 
</file context>
Suggested change
return re.compile(regex_pattern, re.IGNORECASE)
return re.compile(regex_pattern, re.IGNORECASE | re.ASCII)



def get_router(routes: list[str]) -> Callable[[str], str | None]:
Expand Down
17 changes: 17 additions & 0 deletions tests/units/test_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ def test_get_router_splat_catchall(path: str, expected: str | None):
assert router(path) == expected


@pytest.mark.parametrize(
("path", "expected"),
[
("/About", "about"),
("/ABOUT/", "about"),
("/Posts/42", "posts/[id]"),
("/team", "Team"),
("/", "index"),
],
)
def test_get_router_ignores_case(path: str, expected: str):
# React Router renders a page for any casing of its path, so the backend
# must resolve the same route to run its on_load events.
router = get_router(["index", "about", "posts/[id]", "Team"])
assert router(path) == expected


@pytest.mark.parametrize(
("path", "expected"),
[
Expand Down