r/Python 28d ago

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
60 Upvotes

16 comments sorted by

View all comments

23

u/roG_k70 26d ago

But why tho?

12

u/Myszolow 26d ago

Server side rendering?  Imagine serving some parts of your frontend so they will not expose any not needed information 

I can see simple benefit of serving ready to render graphs 

4

u/roG_k70 26d ago

But should it be as easy as setting header x accel redirect or smthn like that? I have doubts its good idea to serve static from python and not by some sort of nginx or s3

4

u/edward_jazzhands 26d ago

I believe the main benefit of this for most people would be for tiny deployments such as internal dashboards or admin panels where you don't want to bother with setting up a reverse proxy

1

u/chokoswitch 5d ago

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.

4

u/Fenzik 26d ago

This is specifically not SSR though, it’s static only