Update app.py
Browse files
app.py
CHANGED
|
@@ -37,6 +37,30 @@ app = Flask(__name__)
|
|
| 37 |
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1)
|
| 38 |
app.secret_key = SECRET_KEY
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# 认证中间件 - 验证所有请求
|
| 41 |
@app.before_request
|
| 42 |
def authenticate():
|
|
|
|
| 37 |
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1)
|
| 38 |
app.secret_key = SECRET_KEY
|
| 39 |
|
| 40 |
+
# 设置静态文件缓存控制
|
| 41 |
+
@app.after_request
|
| 42 |
+
def add_cache_headers(response):
|
| 43 |
+
"""为静态资源添加缓存控制头"""
|
| 44 |
+
if request.path.startswith('/static/'):
|
| 45 |
+
# 设置缓存时间 - CSS、JS和图片缓存1年
|
| 46 |
+
max_age = 31536000 # 1年的秒数
|
| 47 |
+
|
| 48 |
+
# 根据文件类型设置不同的缓存策略
|
| 49 |
+
if request.path.endswith(('.css', '.js')):
|
| 50 |
+
# CSS和JS文件缓存1年
|
| 51 |
+
response.headers['Cache-Control'] = f'public, max-age={max_age}'
|
| 52 |
+
elif request.path.endswith(('.png', '.jpg', '.jpeg', '.gif', '.ico', '.svg')):
|
| 53 |
+
# 图片文件缓存1年
|
| 54 |
+
response.headers['Cache-Control'] = f'public, max-age={max_age}'
|
| 55 |
+
else:
|
| 56 |
+
# 其他静态文件缓存1周
|
| 57 |
+
response.headers['Cache-Control'] = 'public, max-age=604800'
|
| 58 |
+
|
| 59 |
+
# 添加其他有用的缓存头
|
| 60 |
+
response.headers['Vary'] = 'Accept-Encoding'
|
| 61 |
+
|
| 62 |
+
return response
|
| 63 |
+
|
| 64 |
# 认证中间件 - 验证所有请求
|
| 65 |
@app.before_request
|
| 66 |
def authenticate():
|