Flask静态文件Cache-Control [英] Flask static file Cache-Control

查看:170
本文介绍了Flask静态文件Cache-Control的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在开发过程中为我的JS文件设置合理的缓存过期。我有标准的设置,HTML,CSS和JS都在 static 目录下。



docs 确实提到了这一点,但对于我的生活,我无法得到这个工作。我已经试过这两种方法隐含,首先

  class MyFlask(flask.Flask):
def get_send_file_max_age(self, name):
如果name.lower()。endswith('。js'):
返回60
返回flask.Flask.get_send_file_max_age(self,name)

app = MyFlask(__ name__)

和$ / b>

< pre $ app.config ['SEND_FILE_MAX_AGE_DEFAULT'] = 60

这两个都没有效果,我的JS文件在/静态仍然回来,默认缓存超时,

pre code高速缓存控制: public,max-age = 43200


然后,我意识到这个问题,并找不到一个在线答案为我工作。我的静态文件不是从Flask提供的! Flask只生成我的HTML。静态文件是由我的Web服务器直接提供的(在我的情况下,Apache可能是Nginx或其他)。



以下是Apache的说明。



首先安装mod_expires模块:

sudo a2enmod expires



然后在你的 .htaccess 文件中添加如下内容:




ExpTypeByType text / cssaccess plus 1 year
ExpiresByType application / javascriptaccess plus 1 year
ExpiresByType image / *access plus 1 year

有关如何在 Apache手册


I'm trying to set a reasonable cache expiry for my JS files while in development. I have the standard setup, where HTML, CSS and JS are living under the static directory.

The docs do mention this, but for the life of me I cannot get this to work. I've tried both methods implied, first

class MyFlask(flask.Flask):
    def get_send_file_max_age(self, name):
        if name.lower().endswith('.js'):
            return 60
        return flask.Flask.get_send_file_max_age(self, name)

app = MyFlask(__name__)

and

app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 60

Both have had no effect, my JS files under /static are still coming back with the default cache timeout,

Cache-Control: public, max-age=43200

Any pointers appreciated.

解决方案

I had this problem and couldn't find an answer online that worked for me.

Then I realised that my static files are not being served from Flask at all! Flask only generates my HTML. The static files are served directly by my web server (Apache in my case, yours might be Nginx or something else).

Here are the instructions for Apache.

First install the mod_expires module:

sudo a2enmod expires

Then add something like this to your .htaccess file:

ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "access plus 1 year"
ExpiresByType image/* "access plus 1 year"

More details on how to configure it in the Apache manual.

这篇关于Flask静态文件Cache-Control的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆