Cache Busting with File Versions
Force browsers to fetch updated assets.
Cache Busting with File Versions is a free Flask Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Browsers Love to Cache
Browsers save assets to load pages faster. That is great until you ship a new style.css and visitors keep seeing the cached old one. 🗂️
The Stale Asset Problem
If the URL stays /static/style.css forever, the browser assumes nothing changed. Your fresh design simply never reaches the user.
Change the URL to Refetch
The fix is cache busting: change the asset URL whenever the file changes. A new URL forces the browser to download again.
A Version Query String
The simplest trick adds a version query parameter. /static/style.css?v=2 is treated as a new resource by the browser.
<link href="{{ url_for('static', filename='style.css') }}?v=2">Bump It on Each Release
Each time you deploy changed CSS, raise the v number. A central variable beats editing the value in many templates by hand.
Use the File's mtime
A smarter bust uses the file modification time. Read getmtime so the version changes only when the file actually does.
import os
v = int(os.path.getmtime("static/style.css"))Let url_for Carry Extras
Any unknown keyword you pass url_for becomes a query parameter, so you can attach a computed version cleanly.
url_for("static", filename="style.css", v=v)
# -> /static/style.css?v=1718000000Content Hashes Are Best
The strongest approach names files by a hash of their contents, like style.abc123.css. New contents mean a new name and a guaranteed refetch.
Hashes Enable Long Caching
Because a hashed name only changes when contents change, you can cache it almost forever. Speed and freshness stop fighting each other.
Build Tools Do the Work
Asset bundlers generate hashed filenames and a manifest mapping logical names to hashed ones, so your templates ask for the current build.
Pick the Right Tier
Start with a simple ?v= version for small projects. Move to content hashes once a build pipeline is worth the setup.
Quick Check
Why does adding ?v=2 to an asset URL make the browser fetch a fresh copy?
Recap: Cache Busting
You learned to change asset URLs so browsers refetch updates: a version query for small apps, content hashes for serious builds. 🚀
Frequently asked questions
Is the “Cache Busting with File Versions” lesson free?
Yes — the full text of “Cache Busting with File Versions” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.
What will I learn in “Cache Busting with File Versions”?
Force browsers to fetch updated assets. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Flask Academy?
No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Cache Busting with File Versions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Flask Academy lesson?
Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The static Folder Convention
- Link Assets with url_for static
- Cache Busting with File Versions
- Static Files in Production Reality