deniz.in

Markets

Weather

Loading weather

· via Vercel blog

Vercel serves FastAPI static files from the CDN, skipping function invocations

Vercel now promotes FastAPI frontends and StaticFiles mounts to its CDN at build time, so those requests are answered without invoking a serverless function.

Vercel serves FastAPI static files from the CDN, skipping function invocations

What changed

Vercel has begun serving static content from FastAPI applications directly from its edge CDN. According to the company's changelog entry, frontends registered with app.frontend() and directories mounted through FastAPI's StaticFiles are now detected at build time and promoted to the CDN. Requests for those paths are answered by the CDN itself, without a Vercel Function being invoked.

How it works

The changelog illustrates the pattern: an application publishes a dist directory at its root with app.frontend("/", directory="dist") and serves additional assets through app.mount("/assets", StaticFiles(directory="assets"), name="assets"). Both the frontend and the StaticFiles mount end up on the CDN after the build.

Vercel notes that FastAPI's routing semantics carry over after promotion. FastAPI evaluates routes in declaration order, so an explicit route declared before a StaticFiles mount keeps priority over any CDN file at the same path. In the changelog's example, an @app.get("/static/manifest.") handler declared ahead of a /static mount continues to be served by the Vercel Function even though the rest of that directory lives on the CDN.

There is also a compatibility detail worth knowing: promoted source directories remain inside the function bundle, so application code can still read from them at runtime. Teams that want those files to exist only on the CDN can set tool.vercel.fastapi.static.exclude = true in pyproject.toml to strip them from the bundle.

What stays on the function

Not everything is eligible for promotion. Vercel says frontends protected by dependency guards stay on the function because the CDN cannot run dependency checks, and static files that sit behind middleware are likewise kept on the function. Two settings in pyproject.toml override the defaults: tool.vercel.fastapi.static.cdn = true forces promotion to the CDN even when dependencies or middleware are involved, while setting the same key to false opts an app out of CDN serving entirely.

Why it matters

Every request that reaches a Python serverless function adds a hop, possible cold-start exposure and billable compute time compared with a CDN hit. Moving static assets and built frontends onto the CDN at build time removes that overhead for the paths users hit most often, such as pages, scripts and stylesheets, leaving the function to handle actual API logic.

The change also narrows a gap between Python apps and the JavaScript frameworks Vercel was built around, where serving static output from the CDN has long been the default. FastAPI developers now get similar behaviour without reworking their routing, since declaration order, dependency guards and middleware semantics still take effect where they matter. For teams running FastAPI on Vercel at meaningful traffic, the practical outcome is fewer function invocations and faster static responses, with the pyproject.toml flags acting as escape hatches for the cases where the function has to stay in the loop.

  • #vercel
  • #fastapi
  • #python
  • #cdn
  • #serverless

Related posts