I have some caching issues. I'm running very small web-application which reads one frame, saves it to the disk and then shows it in browsers window.
I know, it is probably not the best solution, but every time I save this read frame with the same name and therefor any browser will cache it.
I tried to use html meta-tags - no success:
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /> <meta http-equiv="Pragma" content="no-cache" /> <meta http-equiv="Expires" content="0" />
Also, I have tried this one (flask-specific):
resp.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" resp.headers["Pragma"] = "no-cache" resp.headers["Expires"] = "0"
This is how I tried to modify resp
headers:
r = make_response(render_template('video.html', video_info=video_info)) r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" r.headers["Pragma"] = "no-cache" r.headers["Expires"] = "0"
Still both Google Chrome and Safari do caching.
What might be the problem here?
To disable caching in Python Flask, we can set the response headers to disable cache. to create the add_header function that adds a few headers to the response after each request is done. We make it run after each request with the @app. after_request decorator.
When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache. You can then close out of Developer Tools.
Along the top of the network panel, there's a checkbox that says “Disable Caching.” This disables browser-level caching, but only as long as the DevTools are open. So it won't affect your normal browsing, but while working with the developer tools you won't have to worry about stale content.
Just use the Cache-Control: no-cache header. Implement it as delegating-Handler and make sure your header is applied (with MS Owin Implementation hook up on OnSendingHeaders() .
OK,
finally it worked with this:
@app.after_request def add_header(r): """ Add headers to both force latest IE rendering engine or Chrome Frame, and also to cache the rendered page for 10 minutes. """ r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" r.headers["Pragma"] = "no-cache" r.headers["Expires"] = "0" r.headers['Cache-Control'] = 'public, max-age=0' return r
If you add this, this function will called after each request done. Please,see here
I would be happy, if anyone could explain me why this headers overwriting did not work from the page handler?
Thank you.
If you have always the same problem, that Flask didn't see the updates in JS and CSS files, that because by default, Flask has as max-age value 12 hours. You can set it to 0 to resolve the problem like this:
app = Flask(__name__) app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
Refer to its documentation for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With