Why does my Flask app not render CSS if I don't clear my cache?

我是研究僧i 提交于 2021-02-04 08:36:27

问题


I've been using Flask and have linked the html I'm rendering to a css stylesheet. I've noticed that whenever I update my CSS code, I have to clear the cache in order to get it to update on the webpage. How can I fix this?


回答1:


I assume you are loading your CSS files with something like:

{{ url_for('static', filename='some/file.css') }}

For this to refresh immediately in development, you should set the following config var to -1:

app.config['SEND_FILE_MAX_AGE_DEFAULT'] = -1

As @Matvei states, this issue is more to do with your browser. To visualise this, open the dev tools, go to the Network tab and highlight the specific CSS file. Then in the section on the right look for the following line under Headers -> Response Headers:

Cache-Control: public, max-age=-1

If the setting has been applied correctly this should show -1. If it shows anything else you need to refresh that specific file, until it does show -1, possibly having to clear your cache. This is because the browser makes the choice of whether to reload the file based on the Cache-Control header.

See my similar answer on this with screencaps and links to the docs.




回答2:


tldr: Refresh the page with Ctrl-F5 (or whatever your keyboard shortcut is for a hard refresh).


Full Answer:

The issue (if you want to call it that) is with your browser, not Flask.

To improve speed, browsers cache data. In your case, your browser is caching the CSS for your web pages. Your browser doesn't know when you update the CSS, which is why you have to clear the cache.

Even if there was a way of disabling this, I wouldn't. Without caching, web browsing would be significantly slower.

Instead of clearing your entire cache, you can do a hard refresh. That will tell the browser to reload all data (including CSS) directly from the server. In Firefox, the shortcut is Ctrl F5.

More info here: https://humaan.com/blog/clear-your-browser-cache-a-quick-guide/



来源:https://stackoverflow.com/questions/61740968/why-does-my-flask-app-not-render-css-if-i-dont-clear-my-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!