0
0
Flaskframework~20 mins

Cache busting strategies in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Busting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why use cache busting in Flask apps?

What is the main reason to implement cache busting strategies in a Flask web application?

ATo reduce server CPU usage by caching database queries
BTo prevent users from accessing the app when offline
CTo speed up the Flask app startup time
DTo ensure users always load the latest version of static files like CSS and JavaScript
Attempts:
2 left
💡 Hint

Think about how browsers handle static files and updates.

component_behavior
intermediate
1:30remaining
Flask static file URL with cache busting

Given this Flask template code snippet, what will be the rendered URL for the static file if the file's last modified time is 1680000000?

<link rel="stylesheet" href="{{ url_for('static', filename='style.css', v=timestamp) }}">

Assume timestamp = 1680000000.

A/static/style.css?v=1680000000
B/static/style.css?version=1680000000
C/static/style.css#1680000000
D/static/style.css?v=
Attempts:
2 left
💡 Hint

Look at the query parameter name used in the code.

📝 Syntax
advanced
2:00remaining
Correct way to add cache busting in Flask static URL

Which of the following Flask template snippets correctly adds a cache busting query parameter using the file's last modified time?

{% raw %}{% set timestamp = os.path.getmtime('static/app.js') %}
<script src="{{ url_for('static', filename='app.js', v=timestamp) }}"></script>{% endraw %}
A
{% raw %}{% set timestamp = os.path.getmtime('static/app.js') %}
&lt;script src="{{ url_for('static', filename='app.js') }}?v={{ timestamp }}"&gt;&lt;/script&gt;{% endraw %}
B{% raw %}<script src="{{ url_for('static', filename='app.js') }}?v={{ os.path.getmtime('static/app.js') }}"></script>{% endraw %}
C
{% raw %}{% set timestamp = os.path.getmtime('static/app.js') %}
&lt;script src="{{ url_for('static', filename='app.js', v=timestamp) }}"&gt;&lt;/script&gt;{% endraw %}
D{% raw %}<script src="{{ url_for('static', filename='app.js', version=os.path.getmtime('static/app.js')) }}"></script>{% endraw %}
Attempts:
2 left
💡 Hint

Remember that template code cannot call Python functions directly without context.

🔧 Debug
advanced
2:00remaining
Why does cache busting not work in this Flask app?

Consider this Flask route snippet:

from flask import Flask, url_for, render_template
import os

app = Flask(__name__)

@app.route('/')
def index():
    timestamp = os.path.getmtime('static/style.css')
    return render_template('index.html', timestamp=timestamp)

And the template snippet:

<link rel="stylesheet" href="{{ url_for('static', filename='style.css', v=timestamp) }}">

Users report their browsers still load old CSS after updates. What is the most likely reason?

AFlask's url_for does not support extra query parameters
BThe static file path in getmtime is incorrect or relative to the wrong directory
CThe template does not include the timestamp variable
DThe query parameter 'v' is ignored by browsers for CSS files
Attempts:
2 left
💡 Hint

Check how file paths are resolved in Python.

state_output
expert
2:30remaining
Output URL with dynamic cache busting in Flask

Given this Flask app snippet:

from flask import Flask, url_for, render_template
import os

app = Flask(__name__)

@app.context_processor
def inject_timestamp():
    try:
        timestamp = int(os.path.getmtime('static/main.js'))
    except FileNotFoundError:
        timestamp = 0
    return dict(timestamp=timestamp)

@app.route('/')
def index():
    return render_template('index.html')

And this template snippet:

<script src="{{ url_for('static', filename='main.js', v=timestamp) }}"></script>

What will be the output URL if the file static/main.js was last modified at UNIX timestamp 1681234567?

A/static/main.js?v=1681234567
B/static/main.js?v=0
C/static/main.js
D/static/main.js?v=
Attempts:
2 left
💡 Hint

Consider how the context processor injects variables into templates.