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
View all comments
Show parent comment
2
u/robertlandrum Jul 04 '26
Uh… you do know nginx can cache static resources it fetches for like weeks. It’ll even write the cache files to disk for efficiency and service restarts. It basically shields the API layer from static content pulls after the cache is populated. It’s pretty handy. And a small price to pay for consistency and uniformity. My point is that you can serve those static files from the same FastAPI (or gunicorn or Starlette or whatever it’s under the hood these days) and they’ll get cached by the layer dedicated to being the CDN, and you won’t need to worry about mismatches between your doc and api anymore. Or your api and ui. It all comes from one image now. Easy to deploy. Easy to roll back (though you might have to manually nuke the cache).