Search code examples
flaskcachingbrowser-cachecache-control

I've tried all the common ways of disabling css caching in Flask, and none of them work for me


I have an html file in /templates/books.html, which contains a <link rel="stylesheet" href="{{ url_for('static', filename="css/style.css") }}">

I have a file static/css/style.css with the following code:

body {
}

so nothing in there. Then, I add one style:

body {
display: none;
}

and it works as expected. But then I remove the style again, but Flask still keeps sending me the old file no matter how many times I change it etc.

I have tried the following, to no avail:

  • Restarting the application
  • Restarting my computer
  • Adding something like ?v=1.0.0 to the end of the filename
  • app.config('SEND_FILE_MAX_AGE_DEFAULT') = 0
  • Setting headers:
@app.after_request
     def add_header(r):                              
         r.headers["Cache-Control"] = "no-cache, no-store, must-
         r.headers["Pragma"] = "no-cache"              
         r.headers["Expires"] = "0"
        r.headers['Cache-Control'] = 'public, max-age=0'
         return r
  • 'Disable caching' in chrome devtools
  • Force reload with Ctrl + F5, Shift + F5, Ctrl + Shift + F5, etc. all the combinations, also tried just straight up clearing my entire cache
  • Different browser, incognito mode, etc
  • html meta tags:
<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" />

Does anyone have an idea what else I should try?


Solution

  • I tried troubleshooting by making a minimal flask project, and did not run into the same issue. Then I tried my original project again, and the issue as magically fixed. Not sure why, but I'll take it. I appreciate this means this question is not useful to anyone else, sorry about that.