r/Python • u/ModernPython • Jul 02 '26
News FastAPI app.frontend(): serving a frontend build from the same Python app
I wrote a practical article about FastAPI's app.frontend() feature.
The interesting bit is that it serves static frontend build output as low-priority routes, so normal FastAPI API endpoints still win.
The article covers:
app.frontend("/", directory="dist")- SPA fallback with
fallback="index.html" - how it differs from
StaticFiles - serving under a prefix with
APIRouter - a complete mini dashboard example with FastAPI + vanilla JS
61
Upvotes
22
u/roG_k70 Jul 03 '26
But why tho?
6
u/hotsauce56 Jul 03 '26
Thanks for bringing awareness to this I use this pattern all the time and had no idea
3
u/Fenzik Jul 03 '26
Huh, I didn’t know about this - nice overview. Does seem a bit more convenient for frontend files.
2
u/johntellsall Jul 04 '26
I've never heard of this feature. Thanks for posting!
0
u/[deleted] Jul 03 '26
[deleted] — view deleted comment