编辑:想出来,按F12,点击网络,选中“禁用缓存”。
我有一个基本的烧瓶服务器,我正在学习d3。问题是chrome给了我一个我正在使用的缓存javascript文件,example.js。
请求方法:GET 状态代码:200 OK(来自内存缓存)
服务器本身正在发送一个非缓存的响应,我可以通过直接查看响应来看到这一点:
/static/example.js
我在application.py中添加了这个以防止缓存。
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
这是整个代码
import os
import re
from flask import Flask, jsonify, render_template, request, url_for
from flask_jsglue import JSGlue
from flask import send_file
# configure application
app = Flask(__name__)
JSGlue(app)
# prevent cached responses
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
@app.route("/<string:filename>")
def main(filename):
"""Render file."""
return render_template(filename)
@app.route("/favicon.ico")
def favicon():
filename = 'images/fav.png'
return send_file(filename, mimetype='image/png')
感谢阅读。
答案 0 :(得分:9)
要阻止来自浏览器的缓存响应: - 按F12或右键单击&gt;检查 - 单击网络选项卡 - 选中“禁用缓存”。
要防止来自服务器的缓存响应:将以下定义添加到application.py:
# prevent cached responses
if app.config["DEBUG"]:
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response