Skip to content

Commit b4652eb

Browse files
rodjafalkoschindler
authored andcommitted
Fix navigating ui.sub_pages in root page with remaining path (zauberzeug#5442)
This bug was uncovered while trying to solve zauberzeug#5437 with PR zauberzeug#5440: When using `ui.run(root)` with `ui.sub_pages()`, clicking navigation links which have a remaining path would crash with `KeyError: 'route'`. This occurred because the root function is served through FastAPI's 404 exception handler rather than a registered route, so `request.scope['route']` doesn't exist. Changed `_other_page_builder_matches_path()` in `SubPagesRouter` to safely access `request.scope['route']` using `.get()` instead of direct dictionary access. When the route key is missing (indicating a 404 handler scenario), the method returns `False`. If the client was created via the 404 handler, no routes matched the initial request, so there cannot be other matching page builders. The early return avoids the KeyError and allows navigation to proceed normally. - [x] I chose a meaningful title that completes the sentence: "If applied, this PR will..." - [x] The implementation is complete. - [x] Pytests have been added. - [x] Documentation not necessary. --------- Co-authored-by: Falko Schindler <[email protected]>
1 parent 19d53f2 commit b4652eb

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

nicegui/sub_pages_router.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ async def _handle_navigate(self, path: str) -> None:
8989

9090
def _other_page_builder_matches_path(self, path: str, client: Client) -> bool:
9191
"""Check if there is any other matching page builder than the one for this client."""
92-
client_route = client.request.scope['route']
92+
client_route = client.request.scope.get('route')
93+
if client_route is None:
94+
return False # NOTE: requests handled by 404 handler (e.g., root pages) have no route key
9395
client_func = getattr(client_route.endpoint, '__func__', client_route.endpoint)
9496

9597
other_routes = [route for route in core.app.routes if isinstance(route, Route)]

0 commit comments

Comments
 (0)