I'm trying to serve a small web app using Sveltekit and FastAPI.
FastAPI provides an api and various functions and the frontend was built with Sveltekit using adapter-static
.
The basic framework is:
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
app = FastAPI()
api = FastAPI(root_path="/api")
# mount the api
app.mount("/api", api)
# mount the sveltekit static files at root
app.mount('/', StaticFiles(directory="./webapp/build/", html=True), name="webapp")
# define the api endpoints
@api.websocket("/something")
async def do_something(request: Request):
body = await request.json()
result = do_something_fancy(body)
return {"result": result}
...
What I'm having trouble with is that the frontend app defines multiple sub pages:
- a main page/dashboard at
localhost:8888
- a settings page at
localhost:8888/settings
- an about page at
localhost:8888/about
Now if I navigate to any of these pages using my svelte app navbar after starting at the root url "/", everything works fine, but if I navigate directly to http://localhost:8888/settings
by entering it in the browser address bar, I get a 404 error and see {"detail":"Not Found"}
.
I also get the {"detail":"Not Found"}
when I hit "Refresh"/Ctrl-r
in my browser when on one of the subpages (settings
or about
), but it works fine at the root url.
I'm pretty sure I'm just missing something simple like adding routing to the static app mount point, but the FastAPI docs don't specify how this should work (OR, it seems to indicate that it should "just work").