function get_file_version_v1
Generates a version string for static files based on their modification time, used for cache busting in web applications.
/tf/active/vicechatdev/vice_ai/new_app.py
90 - 102
simple
Purpose
This function provides a cache-busting mechanism for static files (CSS, JavaScript, images) in Flask applications. It returns a version string based on the file's last modification time, which can be appended to URLs as a query parameter. When the file changes, the version string changes, forcing browsers to fetch the updated file instead of using a cached version. Falls back to current timestamp if the file cannot be accessed.
Source Code
def get_file_version(filename):
"""Get version string for static files based on modification time"""
try:
# Try to get file modification time for cache busting
file_path = os.path.join(app.static_folder, filename)
if os.path.exists(file_path):
mtime = os.path.getmtime(file_path)
return str(int(mtime))
except (OSError, AttributeError):
pass
# Fallback to current timestamp
return str(int(time.time()))
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
filename |
- | - | positional_or_keyword |
Parameter Details
filename: The relative path/name of the static file within the Flask application's static folder. Should be a string representing the file path (e.g., 'css/style.css', 'js/app.js'). The function will join this with app.static_folder to get the full path.
Return Value
Returns a string representation of an integer timestamp. If the file exists and is accessible, returns the file's modification time as a Unix timestamp (seconds since epoch) converted to a string. If the file doesn't exist or cannot be accessed, returns the current time as a Unix timestamp string. This value is typically used as a version parameter in URLs (e.g., '/static/style.css?v=1234567890').
Dependencies
flaskostime
Required Imports
import os
import time
from flask import Flask
Usage Example
from flask import Flask
import os
import time
app = Flask(__name__)
def get_file_version(filename):
try:
file_path = os.path.join(app.static_folder, filename)
if os.path.exists(file_path):
mtime = os.path.getmtime(file_path)
return str(int(mtime))
except (OSError, AttributeError):
pass
return str(int(time.time()))
# Usage in Flask route or template context
version = get_file_version('css/style.css')
static_url = f'/static/css/style.css?v={version}'
# Or register as template filter
@app.template_filter('file_version')
def file_version_filter(filename):
return get_file_version(filename)
# Then in Jinja2 template:
# <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}?v={{ 'css/style.css'|file_version }}">
Best Practices
- This function requires access to a Flask app instance with a configured static_folder attribute
- The function gracefully handles missing files and permission errors by falling back to current timestamp
- Best used as a Jinja2 template filter or context processor for automatic version string generation in templates
- The returned version string should be appended to static file URLs as a query parameter (e.g., ?v=timestamp)
- Consider caching the results of this function in production to avoid repeated file system calls
- Ensure the static folder has appropriate read permissions for the application user
- The fallback to current timestamp means every request will generate a new version if the file doesn't exist, which may not be ideal for production
- For production environments with multiple servers, consider using a consistent versioning strategy (like git commit hash) instead of file modification time
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_file_version 94.5% similar
-
function get_cache_buster 69.9% similar
-
function inject_template_vars 65.1% similar
-
function debug_cache_info 63.0% similar
-
function touch_static_files 61.3% similar