Skip to main content

Flask extension for serving Vite-managed assets in development and production

Project description

Flask-Vite-Assets

A Flask extension that seamlessly integrates Vite asset management into your Flask/Jinja2 templates, supporting both the Vite dev server (with HMR) and production manifest-based asset fingerprinting.

Features

  • 🔥 Hot Module Replacement (HMR) in development via the Vite dev server
  • 📦 Manifest-based asset fingerprinting in production
  • 🎨 Automatic CSS injection from the Vite manifest
  • Cache-busting url_for — static file URLs automatically gain a ?v=<mtime> parameter so browsers always pick up the latest file after a deployment
  • 🔌 Flask extension pattern with init_app support
  • 🧩 Zero-config defaults with full configurability
  • 🐍 Typed — ships with py.typed

Installation

pip install flask-vite-assets

If you use the Vite dev server in development, also install requests:

pip install "flask-vite-assets[dev-server]"

Quick start

Application factory pattern

from flask import Flask
from flask_vite_assets import ViteAssets

vite = ViteAssets()

def create_app():
    app = Flask(__name__)
    vite.init_app(app)
    return app

Direct initialisation

from flask import Flask
from flask_vite_assets import ViteAssets

app = Flask(__name__)
ViteAssets(app)

Jinja2 templates

<!DOCTYPE html>
<html>
<head>
    <!-- Injects <link> + <script> tags from the Vite manifest in production,
         or a <script> tag pointing at the Vite dev server in development -->
    {{ vite_asset("js/main.js") }}

    <!-- Cache-busting url_for: appends ?v=<mtime> to static file URLs so
         browsers always fetch the latest file after a deployment -->
    <link rel="stylesheet" href="{{ url_for('static', filename='css/extra.css') }}">
</head>
<body>
    ...
    <!-- Injects the Vite HMR client in development only (no-op in production) -->
    {{ vite_hmr_client() }}
</body>
</html>

How it works

Development mode (DEBUG=True)

  1. The extension attempts to connect to the Vite dev server (default http://localhost:5173).
  2. If the server is reachable it returns a <script type="module"> tag pointing at the dev server, enabling HMR.
  3. If the primary port is unavailable it automatically tries the next port (5174), which Vite uses when the preferred port is already occupied.
  4. If neither server is reachable it falls back to the production manifest.

Production mode (DEBUG=False)

  1. Reads <app.static_folder>/.vite/manifest.json.
  2. For each entry emits <link rel="stylesheet"> tags for associated CSS files, then a <script type="module"> tag for the JS bundle.
  3. Falls back to serving the asset path directly if the manifest is missing or malformed.

Configuration

All configuration is optional.

Flask config key Environment variable Default Description
VITE_DEV_SERVER VITE_DEV_SERVER http://localhost:5173 Base URL of the Vite dev server
VITE_MANIFEST_PATH <app.static_folder>/.vite/manifest.json Absolute path to the production Vite manifest

Flask config takes precedence over environment variables.

Vite configuration

Point Vite's output at your Flask static folder and enable manifest generation:

// vite.config.js
import { defineConfig } from "vite";

export default defineConfig({
  root: "resources",
  base: "/static/",
  build: {
    outDir: "../resources/static",
    manifest: true,
    rollupOptions: {
      input: "assets/js/main.js",
    },
  },
});

Using the helpers directly

You can also call the helpers directly in Python code instead of going through the extension:

from flask_vite_assets import vite_asset, vite_hmr_client

License

MIT

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

flask_vite_assets-0.2.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

flask_vite_assets-0.2.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file flask_vite_assets-0.2.0.tar.gz.

File metadata

  • Download URL: flask_vite_assets-0.2.0.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for flask_vite_assets-0.2.0.tar.gz
Algorithm Hash digest
SHA256 aa9ed7923c9f25c0e7baaf5c7023e7250399ac7d7052246356e247264d3ae019
MD5 e02b87ec49e042291bfcf205721ee19f
BLAKE2b-256 9adefd7571f3147583bdd9b7e938229fcb85aaf46ad081c318e30e9d5c8e6fd4

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_vite_assets-0.2.0.tar.gz:

Publisher: release.yml on mrailton/flask-vite-assets

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flask_vite_assets-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_vite_assets-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c508563a25c5974a44293af5089ebd9c9e700826d2cc7c4c8e0f65bda341439a
MD5 98d3b62fe3f4c592f423de666e15f13c
BLAKE2b-256 72e6c1f5d0d36e0318364c609b81c5929f926092a5e5a06a0d4df01f5e8541b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_vite_assets-0.2.0-py3-none-any.whl:

Publisher: release.yml on mrailton/flask-vite-assets

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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