Static Files
Flask serves CSS, JavaScript, images, and other static assets from the static/ folder under the application directory, mounted at the /static/ URL prefix.
Directory Structure
Referencing in Templates
Always generate static URLs with url_for('static', filename=...) instead of hardcoding paths — templates then keep working when you move the static directory or add a CDN prefix:
Custom Static Directory and URL Prefix
Blueprints can carry their own static directories for module-level asset isolation:
Cache Control
During development, browser caching often hides your CSS changes. Control the static cache lifetime:
User Uploads and send_from_directory
User-uploaded files should not live in static/ (to avoid uncontrolled access). Serve them through a controlled view:
Production Best Practices
- Don't serve heavy static traffic from the Flask process: let Nginx/Caddy serve the
static/directory directly, or use a CDN. - Fingerprinting (cache busting): frontend build tools (Vite/Webpack) hash filenames (e.g.
app.3f8a2c.js), so a one-year cache is safe. - Compression: enable gzip/brotli at the proxy layer rather than in Flask.
Nginx example: