0
0
Flaskframework~10 mins

Cache busting strategies in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Cache busting strategies
Client requests resource
Check resource URL
Is URL unique?
NoServe cached resource
Yes
Serve fresh resource
Update cache with new resource
This flow shows how cache busting works by changing resource URLs so browsers fetch fresh files instead of cached ones.
Execution Sample
Flask
from flask import Flask, url_for
app = Flask(__name__)

@app.context_processor
def override_url_for():
    return dict(url_for=dated_url_for)

def dated_url_for(endpoint, **values):
    if endpoint == 'static':
        filename = values.get('filename', '')
        import os
        filepath = os.path.join(app.root_path, 'static', filename)
        if os.path.exists(filepath):
            values['q'] = int(os.path.getmtime(filepath))
    return url_for(endpoint, **values)
This Flask code adds a timestamp query to static file URLs to force browsers to load updated files.
Execution Table
StepActionInput URLFile Modified TimeOutput URLCache Behavior
1Request static file 'style.css'/static/style.css1686000000/static/style.css?q=1686000000Browser treats URL as new, fetches fresh file
2Request static file 'style.css' again without file change/static/style.css1686000000/static/style.css?q=1686000000Browser uses cached file because URL unchanged
3File 'style.css' updated (mtime changes)/static/style.css1686000500/static/style.css?q=1686000500Browser sees new URL, fetches updated file
4Request non-static endpoint/homeN/A/homeNo cache busting, normal caching applies
5Request static file that does not exist/static/missing.jsN/A/static/missing.jsNo timestamp added, browser caches normally
💡 Execution stops after requests are handled; cache busting applies only to existing static files.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5
filename'''style.css''style.css''missing.js'
filepath'''/app/static/style.css''/app/static/style.css''/app/static/missing.js'
os.path.getmtime(filepath)N/A16860000001686000500N/A
values['q']N/A16860000001686000500N/A
Output URLN/A/static/style.css?q=1686000000/static/style.css?q=1686000500/static/missing.js
Key Moments - 3 Insights
Why does the URL get a '?q=timestamp' query parameter?
The timestamp query '?q=timestamp' changes the URL whenever the file changes, making the browser fetch the new file instead of using the cached one. See execution_table rows 1 and 3.
What happens if the static file does not exist?
No timestamp is added because the file modification time can't be found. The URL stays the same, so the browser may use a cached version or fail. See execution_table row 5.
Does cache busting apply to non-static URLs?
No, only static files get the timestamp query. Other URLs like '/home' are unaffected and use normal caching. See execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output URL at step 3 when the file is updated?
A/static/style.css?q=1686000500
B/static/style.css?q=1686000000
C/static/style.css
D/static/style.css?q=0
💡 Hint
Check the 'Output URL' column in execution_table row 3.
At which step does the browser use the cached file without fetching a new one?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look for 'Cache Behavior' that says browser uses cached file in execution_table.
If the file modification time never changes, what will happen to the URL query parameter?
AIt will keep changing every request
BIt will be removed
CIt will stay the same
DIt will cause an error
💡 Hint
Refer to variable_tracker for 'values["q"]' after steps 1 and 2.
Concept Snapshot
Cache busting in Flask:
- Add file modification time as query param to static URLs
- Use a custom url_for function override
- Browser treats changed URLs as new, fetches fresh files
- Helps avoid stale cached static files
- Non-static URLs unaffected
- Missing files get no timestamp
Full Transcript
Cache busting strategies in Flask involve changing the URL of static files by adding a query parameter with the file's last modified time. This forces browsers to fetch the updated file instead of using a cached version. The provided code overrides Flask's url_for function to append this timestamp for static files. When a client requests a static file, the server checks the file's modification time and adds it as a query parameter. If the file changes, the URL changes, so the browser fetches the new file. If the file does not exist, no timestamp is added. Non-static URLs are not affected by this strategy. This approach helps keep static resources fresh without disabling caching entirely.