Flask静态文件Cache-Control

时间:2012-07-06 05:13:30

标签: python flask cache-control static-files

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

docs确实提到了这一点,但对于我的生活,我无法让它发挥作用。我已经尝试过隐含的两种方法,首先

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__)

app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 60

两者都没有效果,我在/ static下的JS文件仍然会返回默认的缓存超时,

Cache-Control: public, max-age=43200

任何指示赞赏。

2 个答案:

答案 0 :(得分:2)

我遇到了这个问题,无法在网上找到适合我的答案。

然后我意识到我的静态文件根本没有从Flask提供! Flask只生成我的HTML。静态文件由我的Web服务器直接提供(在我的情况下是Apache,你的可能是Nginx或其他东西)。

以下是Apache的说明。

首先安装mod_expires模块:

sudo a2enmod expires

然后将这样的内容添加到.htaccess文件中:

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

有关如何在Apache manual中配置它的详细信息。

答案 1 :(得分:0)

您可能需要查看webassets来管理缓存过期。它适用于开发和生产环境。

相关问题