`ui.sub_pages` stays in the SPA instead of loading pages registered via `app.include_router()`
Filed by Claude Code on @jnossan's behalf, who reported it in #5777.
First Check
- I added a very descriptive title here.
- This is not a security issue (those should be reported via the security advisory instead).
- This is not a Q&A. I am sure something is wrong with NiceGUI or its documentation.
- I used the GitHub search to find a similar issue and came up empty.
Example Code
from nicegui import APIRouter, app, ui
router = APIRouter(prefix='/other')
@router.page('/')
def other_page():
ui.label('Other')
app.include_router(router)
@ui.page('/')
def index():
ui.sub_pages({'/': lambda: ui.label('Home')})
ui.button('Go to /other', on_click=lambda: ui.navigate.to('/other'))
ui.link('Go to /other', '/other')
ui.run()Description
Expected: clicking either the button or the link leaves the SPA and performs a full page load of /other, the same way it does for a page registered with plain @ui.page('/other').
Actual: ui.sub_pages keeps handling the path, finds no matching sub page, and shows its 404 inside the container. The URL /other is perfectly valid on the server — GET /other answers 307 to /other/, which answers 200 — so only the client-side navigation is broken.
This is the same symptom as #5777, which was fixed by SubPagesRouter._other_page_builder_matches_path() in nicegui/sub_pages_router.py. That check asks every route in core.app.routes whether it matches the target path, and falls back to client.open() when one does. It misses routes coming from an APIRouter for two independent reasons:
1. The trailing slash. APIRouter(prefix='/other') plus @router.page('/') registers the path /other/. Route.matches() is literal and knows nothing about FastAPI's redirect_slashes, so /other yields Match.NONE and the router assumes the path is its own. Navigating to /other/ works around it. This is not an exotic spelling: our own documentation and the modularization example both link to the prefix without a trailing slash.
2. FastAPI's lazy router inclusion. Since FastAPI 0.141, app.include_router() appends a single _IncludedRouter wrapper to app.routes instead of the individual routes. The wrapper is not a starlette.routing.Route, so the isinstance(route, Route) filter drops it and the check sees no page routes at all — with FastAPI 0.141, even /other/ stays inside the SPA:
$ python -c "
from nicegui import APIRouter, app, ui, core
router = APIRouter(prefix='/other')
@router.page('/')
def other_page(): ui.label('Other')
app.include_router(router)
print([getattr(r, 'path', r) for r in core.app.routes][-1:])
"
[_IncludedRouter(original_router=<nicegui.api_router.APIRouter object at 0x102e3b2f0>, …)]Routing itself is unaffected — FastAPI resolves requests through the wrapper — but every place in NiceGUI that inspects core.app.routes goes blind. Besides the check above, that is App.remove_route() (nicegui/app/app.py), the endpoint_documentation loop in nicegui/ui_run.py, and nicegui_reset_globals() in nicegui/testing/general.py. Our uv.lock pins FastAPI 0.136.3, so CI never exercises this, while pyproject.toml allows fastapi>=0.109.1 and users get 0.141.
Workaround
Pass the page function instead of the path — ui.navigate.to() only hands string targets to the SPA router:
ui.button('Go to /other', on_click=lambda: ui.navigate.to(other_page))NiceGUI Version
3.15.0
Python Version
3.12.9
Browser
Chrome
Operating System
macOS
Additional Context
Verified with browser tests against the current main (3.15.0.post52.dev0+a2371a6f5, FastAPI 0.136.3): navigating to /other by button or by link fails, /other/ and the page-function form succeed. Cause 2 verified separately against released NiceGUI 3.15.0 with FastAPI 0.141.1, where _other_page_builder_matches_path() returns False for both /other and /other/.
Source: zauberzeug/nicegui