Commit 31c4a69
Fix 404 handler swallowing SPA fallback under ui.run_with(root=...) (#5999)
### Motivation
Closes #5998.
#5974 changed the 404 handler to return JSON when an endpoint matched
but no `nicegui_page_path` was set, so that `@app.get` routes raising
`HTTPException(404)` would no longer be served NiceGUI's HTML error
page. The discriminator was `'endpoint' in request.scope`.
That works under plain `ui.run()`. Under `ui.run_with(parent_app,
root=root)` it breaks SPA fallback: Starlette's `Mount.matches()`
populates `scope["endpoint"]` with the mounted app reference _before_
the inner router runs (see [starlette/routing.py
L429](https://github.com/encode/starlette/blob/0.52.1/starlette/routing.py#L429)).
When the inner router doesn't match anything (e.g. `/auth/login` is only
known to a client-side `ui.sub_pages`), that `endpoint` survives into
the 404 handler. The new branch then incorrectly fires and returns
`{"detail":"Not Found"}` instead of falling through to render `root`.
### Implementation
Tighten the discriminator: only treat the request as "an endpoint raised
404" when `scope["endpoint"]` is set _and_ is not `core.app` itself. A
real inner `Route` match overwrites `endpoint` with the handler
function, so this distinguishes the two cases cleanly.
Added two regression tests in `tests/test_run_with.py` exercising the
mounted-app code path that the existing `tests/test_page.py` tests don't
cover:
- `test_run_with_unknown_path_falls_through_to_root` — the actual
regression guard (fails on `main`)
- `test_run_with_api_endpoint_404_still_returns_json` — guards #5974's
intent under `ui.run_with`
### Progress
- [x] The PR title is a short phrase starting with a verb like "Add
...", "Fix ...", "Update ...", "Remove ...", etc.
- [x] The implementation is complete.
- [x] This PR does not address a security issue.
- [x] Pytests have been added.
- [x] Documentation is not necessary.
- [x] No breaking changes to the public API.
---------
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: Evan Chan <evan@nicegui.io>1 parent b29ab8b commit 31c4a69
2 files changed
Lines changed: 35 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
166 | | - | |
| 166 | + | |
167 | 167 | | |
168 | 168 | | |
| 169 | + | |
169 | 170 | | |
170 | 171 | | |
171 | 172 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
0 commit comments