Skip to main content

Serves Vue assets on a FastAPI app. Use fastapi-vue-setup tool to add Vue build to your package.

Project description

fastapi-vue

Runtime helpers for FastAPI + Vite/Vue projects.

Overview

This package provides:

  • fastapi_vue.Frontend: serves built SPA assets (with SPA support, caching, and optional zstd)
  • fastapi_vue.server.run: a small Uvicorn runner with convenient listen endpoint parsing

Quickstart

Serve built frontend assets from frontend-build/:

from pathlib import Path
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi_vue import Frontend

frontend = Frontend(Path(__file__).with_name("frontend-build"), spa=True)

@asynccontextmanager
async def lifespan(app: FastAPI):
    await frontend.load()
    yield

app = FastAPI(lifespan=lifespan)

# Add API routes here...

# Final catch-all route for frontend files (keep at end of file)
frontend.route(app, "/")

Frontend

Frontend serves a directory with:

  • RAM caching, with zstd compression when smaller than original
  • Browser caching: ETag + Last-Modified, Immutable assets
  • Favicon mapping (serve PNG or other images there instead)
  • SPA routing (serve browsers index.html at all paths not otherwise handled)

Dev-mode behavior with FastAPI(debug=True): requests error HTTP 409 with a message telling you to use the Vite dev server instead. Avoids accidentally using outdated frontend-build during development.

  • directory: Path on local filesystem
  • index: Index file name (default: index.html)
  • spa: Serve index at any path (default: False)
  • catch_all: Register a single catch-all handler instead of a route to each file; default for SPA
  • cached: Path prefixes treated as immutable (default: /assets/)
  • favicon: Optional path or glob (e.g. /assets/logo*.png)
  • zstdlevel: Compression level (default: 18)

ℹ️ Even when your page has a meta tag giving favicon location, browsers still try loading /favicon.ico whenever looking at something else. We find it more convenient to simply serve the image where the browser expects it, with correct MIME type. This also allows having a default favicon for your application that can be easily overriden at the reverse proxy (Caddy, Nginx) to serve the company branding if needed in deployment.

Server runner

When you need more flexibility than fastapi CLI can provide (e.g. CLI arguments to your own program), you may use this convenience to run FastAPI app with Uvicorn startup on given listen endpoints. Runs in the same process if possible but delegates to uvicorn.run() for auto-reloads and multiple workers. This would typically be called from your CLI main, which can set its own env variables to pass information to the FastAPI instances that run (Python imports only work in same-process mode).

from fastapi_vue import server

server.run("my_app.app:app", listen=["localhost:8000"])
  • As a deployment option, environment FORWARDED_ALLOW_IPS controls X-Forwarded trusted IPs (default: 127.0.0.1,::1).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_vue-1.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_vue-1.1.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_vue-1.1.0.tar.gz.

File metadata

  • Download URL: fastapi_vue-1.1.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastapi_vue-1.1.0.tar.gz
Algorithm Hash digest
SHA256 770f71c22fb084f96210fbc4a0d7efb072c4b76b6d0b157e9230d1d63ebe4552
MD5 874f800a52c6735d25d0123e6fe2da8c
BLAKE2b-256 f528b5ddb63b43cd2b21d3128e6af85a02c6cde72d7eb387aa61ea38e0bd5ee1

See more details on using hashes here.

File details

Details for the file fastapi_vue-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: fastapi_vue-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for fastapi_vue-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ff6b367bd4f32c80838615980ee43d5516e10bbeef052f2c9b881c9b134391e2
MD5 dacbe4d47898d88d813fffe2a4784cb7
BLAKE2b-256 9a0951d714be92a5bd7f091891cb288c6a77d33bc31a03c9508ed7fed0ea1b49

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page