Cache busting helps your website show the newest files to visitors by avoiding old saved versions in their browsers.
Cache busting strategies in Flask
url_for('static', filename='style.css', v='12345')
url_for('static', filename='app.js', v='20240601')
url_for('static', filename='main.css', v='abcdef')
url_for('static', filename='image.png') + '?v=' + str(time.time())
This Flask app serves a simple page that includes CSS and JS files with a version query parameter. This helps browsers load the latest files when the version changes.
from flask import Flask, render_template_string, url_for import time app = Flask(__name__) @app.route('/') def index(): version = '20240601' # Example version string css_url = url_for('static', filename='style.css', v=version) js_url = url_for('static', filename='app.js', v=version) html = f''' <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Cache Busting Example</title> <link rel="stylesheet" href="{css_url}"> </head> <body> <h1>Cache Busting with Flask</h1> <script src="{js_url}"></script> </body> </html> ''' return render_template_string(html) if __name__ == '__main__': app.run(debug=True)
Use a fixed version string that you update when files change, like a date or a hash.
Avoid using constantly changing values like timestamps in production because it disables caching benefits.
Flask's url_for is the best way to generate URLs for static files with cache busting.
Cache busting forces browsers to load updated static files by changing their URLs.
Use version query parameters with url_for in Flask to add cache busting.
Update the version string only when files change to keep caching effective.
Practice
Solution
Step 1: Understand cache behavior in browsers
Browsers cache static files to avoid re-downloading them on every visit, which improves speed.Step 2: Identify the role of cache busting
Cache busting changes the URL of static files so browsers fetch the new version instead of using the cached one.Final Answer:
To force browsers to load updated static files by changing their URLs -> Option AQuick Check:
Cache busting = force reload updated files [OK]
- Confusing cache busting with server-side caching
- Thinking cache busting compresses files
- Believing cache busting blocks file access
url_for function?Solution
Step 1: Recall how
Any extra keyword arguments inurl_foradds query parametersurl_forbecome query parameters in the URL.Step 2: Identify the common cache busting parameter name
v='1.0'is the common short form used for cache busting, adding ?v=1.0.Final Answer:
url_for('static', filename='style.css', v='1.0')-> Option CQuick Check:
Extra args in url_for = query params [OK]
- Putting version query string inside filename argument
- Using special parameters like _anchor (adds fragment, not query)
- Using _external (absolute URL, no query param)
<link rel="stylesheet" href="{{ url_for('static', filename='app.css', v='2.1') }}">What will be the rendered href attribute in the HTML?
Solution
Step 1: Understand how
url_forbuilds URLsurl_for('static', filename='app.css', v='2.1')creates a URL to the static file with a query parameterv=2.1.Step 2: Construct the full URL
The URL will be/static/app.css?v=2.1becausefilenamesets the path andvadds the query string.Final Answer:
/static/app.css?v=2.1 -> Option DQuick Check:
url_for adds query param v=2.1 [OK]
- Confusing parameter name 'v' with 'version'
- Forgetting the query parameter appears in URL
- Assuming no query string is added
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?Solution
Step 1: Understand cache busting requires version change
Browsers cache files by URL. If the version string stays the same, the URL doesn't change, so the browser uses cached files.Step 2: Identify why old styles load
If the version string is not updated after file changes, the browser won't request the new file.Final Answer:
You forgot to update the version string to a new value -> Option BQuick Check:
Version string must change to bust cache [OK]
- Assuming Flask ignores query parameters in url_for
- Not verifying static folder presence
- Changing filename without updating version
Solution
Step 1: Consider manual version updates
Manually updating version strings is error-prone and tedious.Step 2: Use file modification time for automation
Using the file's last modified timestamp as a query parameter ensures the URL changes only when the file changes, automating cache busting.Step 3: Evaluate other options
Disabling caching hurts performance; renaming files randomly breaks references and is inefficient.Final Answer:
Use the file's last modified timestamp as a query parameter inurl_for-> Option AQuick Check:
File timestamp automates cache busting [OK]
- Relying on manual version updates only
- Disabling caching instead of busting
- Random renaming causing broken links
Ready to prove it?
Pass the test and this pattern moves unknown
