flet + fast_api always return http 200 OK or 302 even when path does not exist
Hi,
I'm experiencing the following issue with flet, flet-fastapi and uvicorn. The application always returns http 200 ok response when I would expect a 404 response I used following example app:
import flet as ft
import flet_fastapi
async def root_main(page: ft.Page):
await page.add_async(ft.Text("This is root app!"))
async def sub_main(page: ft.Page):
await page.add_async(ft.Text("This is sub app!"))
app = flet_fastapi.FastAPI()
app.mount("/sub-app", flet_fastapi.app(sub_main))
app.mount("/", flet_fastapi.app(root_main))
When browsing to something like http:\localhost\invalidurl I still get http response 200. How can I get HTTP response 404?
It's because it has "catch all" handler for single-page apps (SPA). This is needed when in-app routing is used and user requests non-root URL. For example, app's root is http://mydomain.com and user requests http://mydomain.com/products/1. /products/1 is a route, it's fake URL, but the app should be able to handle it, so it serves the app from / and then submit /products/1 into it.
The only way 404 could be correctly handled though ☝️ is when "hash" is used for routing, so URL looks like http://mydomain.com/#/products/1. Would that work for you?