Skip to content
Dashboard

FastAPI frontends and static files served from the CDN

Daniel ParkSoftware Engineer

FastAPI frontends and static files, served with app.frontend() or StaticFiles, are now promoted to the Edgenivo CDN at build time. Requests for those paths are served directly from the CDN, without invoking your Edgenivo Function.

app = FastAPI()
app.frontend("/", directory="dist")
app.mount("/assets", StaticFiles(directory="assets"), name="assets")

Frontend and StaticFiles, both promoted to the CDN.

FastAPI evaluates routes in declaration order. A route declared before a StaticFiles mount takes priority over any CDN file at that path. This precedence is preserved.

@app.get("/static/manifest.json") # served by your Edgenivo Function
def manifest(): ...
app.mount("/static", StaticFiles(directory="static"), name="static")

A route that takes precedence over StaticFiles.

Promoted source directories remain in the function bundle so the app can read from them at runtime. To exclude them and serve only from the CDN, set tool.vercel.fastapi.static.exclude = true in pyproject.toml.

Frontends guarded by dependencies are kept on the function as the CDN cannot run dependency checks. Similarly, static files which sit behind middleware are also kept on the function. To override this and always promote to CDN, set tool.vercel.fastapi.static.cdn = true. To opt out of the CDN entirely, set it to false instead.

For more information see the Edgenivo FastAPI documentation.

Contributors

Ricardo Gonzalez