Serving Static Files from FastAPI Frontends via CDN
Key point
Vercel promotes FastAPI frontends and static files to the CDN at build time, serving them without function invocations.
Details
Vercel has introduced a feature that promotes the frontend and static files (app.frontend() or StaticFiles) of FastAPI applications to the CDN at build time for serving. Requests to these paths are now handled directly by the CDN without invoking a Vercel Function.
Routing Priority and Exception Handling
Since FastAPI evaluates routes in the order they are declared, routes declared before a StaticFiles mount take precedence over CDN files for that path. This priority is maintained.
The original source directory remains in the function bundle so the app can read it at runtime, but setting tool.vercel.fastapi.static.exclude = true in pyproject.toml allows excluding it and serving it exclusively from the CDN.
Dependency and Middleware Handling
The CDN cannot perform dependency checks, so frontends protected by dependencies remain in the function. Static files behind middleware also remain in the function. Setting tool.vercel.fastapi.static.cdn = true overrides this behavior and always promotes them to the CDN, while setting it to false completely disables CDN usage.
This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.
Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.