0
0
Flaskframework~10 mins

Cache busting strategies in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a version query parameter for cache busting in a Flask template URL.

Flask
url_for('static', filename='style.css') + '?v=[1]'
Drag options to blanks, or click blank then click option'
Aversion
Bcache
Crandom
Dtimestamp
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed string that never changes, so cache is not busted.
Forgetting to add the query parameter at all.
2fill in blank
medium

Complete the Flask route to send a static file with a cache-busting header.

Flask
from flask import send_from_directory, make_response

@app.route('/static/<path:filename>')
def static_files(filename):
    response = make_response(send_from_directory('static', filename))
    response.headers['Cache-Control'] = '[1]'
    return response
Drag options to blanks, or click blank then click option'
Ano-cache, no-store, must-revalidate
Bmax-age=3600
Cpublic, max-age=86400
Dprivate, max-age=0
Attempts:
3 left
💡 Hint
Common Mistakes
Using a long max-age which allows caching.
Not setting Cache-Control header at all.
3fill in blank
hard

Fix the error in the Flask template to append a file hash for cache busting.

Flask
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}?v=[1]">
Drag options to blanks, or click blank then click option'
Ahash
Bhash_file
Cget_hash()
Dfile_hash
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function call inside the template variable.
Using a variable name not passed from the view.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps filenames to their cache-busted URLs using a version number.

Flask
cache_busted_urls = {file: url_for('static', filename=file) + '?v=' + [1] for file in files if file.[2]('.css')}
Drag options to blanks, or click blank then click option'
Aversion
Bendswith
Cstartswith
Din
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'startswith' instead of 'endswith' for file extension.
Not appending the version parameter.
5fill in blank
hard

Fill all three blanks to create a Flask function that returns a static file URL with a timestamp cache buster and sets a no-cache header.

Flask
from flask import url_for, make_response, send_from_directory
import time

def get_static_file(filename):
    url = url_for('static', filename=filename) + '?v=' + str([1])
    response = make_response(send_from_directory('static', filename))
    response.headers['Cache-Control'] = '[2]'
    return response, url

current_time = [3]
Drag options to blanks, or click blank then click option'
Atime.time()
Bno-cache, no-store, must-revalidate
Cint(time.time())
Dmax-age=3600
Attempts:
3 left
💡 Hint
Common Mistakes
Using float timestamp which may cause URL issues.
Setting Cache-Control to allow caching.