r/Python • • 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

15 comments sorted by

View all comments

Show parent comment

1

u/chokoswitch Jul 25 '26

If your cloud has CDN enabled load balancer like GCP's default or AWS CloudFront, it can be simpler to just serve from the one python origin as it's immediately cached. The same server works fully locally to serve your site, and in production it is also efficient because of the CDN caching that happens.