fix: 添加禁用缓存响应头解决浏览器缓存问题
- 为 HTML 页面添加 Cache-Control: no-cache - 添加 Pragma: no-cache - 添加 Expires: 0 - 解决用户浏览器缓存旧版本 HTML 的问题
This commit is contained in:
12
app.py
12
app.py
@@ -6,7 +6,7 @@
|
||||
作者:小白 🐶
|
||||
"""
|
||||
|
||||
from flask import Flask, render_template, redirect, url_for, request, flash
|
||||
from flask import Flask, render_template, redirect, url_for, request, flash, make_response
|
||||
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from werkzeug.security import generate_password_hash, check_password_hash
|
||||
@@ -18,6 +18,16 @@ app.config['SECRET_KEY'] = 'xiaobai-secret-key-2026'
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
|
||||
# 禁用缓存 - 解决浏览器缓存问题
|
||||
@app.after_request
|
||||
def add_no_cache_headers(response):
|
||||
"""为 HTML 页面添加禁用缓存的响应头"""
|
||||
if 'text/html' in response.content_type:
|
||||
response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
|
||||
response.headers['Pragma'] = 'no-cache'
|
||||
response.headers['Expires'] = '0'
|
||||
return response
|
||||
|
||||
# 初始化数据库
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user