Performance: Cache busting strategies
This affects how quickly updated resources load and how browsers cache static files, impacting page load speed and user experience.
from flask import url_for # In templates or code, use version query param url_for('static', filename='style.css', v='12345') # Or use file hash as version to bust cache automatically
app = Flask(__name__) @app.route('/static/<path:filename>') def static_files(filename): return send_from_directory('static', filename) # No cache busting, browser may load old cached files
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No cache busting (static URLs) | Minimal | 0 | Low | [X] Bad |
| Cache busting with query version | Minimal | 0 | Low | [OK] Good |
| Cache busting with file hash in filename | Minimal | 0 | Low | [OK] Good |
url_for function?url_for adds query parametersurl_for become query parameters in the URL.v='1.0' is the common short form used for cache busting, adding ?v=1.0.url_for('static', filename='style.css', v='1.0') -> Option C<link rel="stylesheet" href="{{ url_for('static', filename='app.css', v='2.1') }}">url_for builds URLsurl_for('static', filename='app.css', v='2.1') creates a URL to the static file with a query parameter v=2.1./static/app.css?v=2.1 because filename sets the path and v adds the query string.v='1.0' in url_for, but after updating your CSS file, the browser still loads the old styles. What is the most likely cause?url_for -> Option APass the test and this pattern moves unknown