bust spa asset cache by build id
This commit is contained in:
@@ -36,10 +36,18 @@ def render_app_spa_or_legacy(
|
||||
logger.warning(f"[app_spa] manifest缺少入口文件: {manifest_path}")
|
||||
return render_template(legacy_template_name, **legacy_context)
|
||||
|
||||
app_spa_js_file = f"app/{js_file}"
|
||||
app_spa_css_files = [f"app/{p}" for p in css_files]
|
||||
app_spa_build_id = _get_asset_build_id(
|
||||
os.path.join(current_app.root_path, "static"),
|
||||
[app_spa_js_file, *app_spa_css_files],
|
||||
)
|
||||
|
||||
return render_template(
|
||||
"app.html",
|
||||
app_spa_js_file=f"app/{js_file}",
|
||||
app_spa_css_files=[f"app/{p}" for p in css_files],
|
||||
app_spa_js_file=app_spa_js_file,
|
||||
app_spa_css_files=app_spa_css_files,
|
||||
app_spa_build_id=app_spa_build_id,
|
||||
app_spa_initial_state=spa_initial_state,
|
||||
)
|
||||
except FileNotFoundError:
|
||||
@@ -50,6 +58,20 @@ def render_app_spa_or_legacy(
|
||||
return render_template(legacy_template_name, **legacy_context)
|
||||
|
||||
|
||||
def _get_asset_build_id(static_root: str, rel_paths: list[str]) -> Optional[str]:
|
||||
mtimes = []
|
||||
for rel_path in rel_paths:
|
||||
if not rel_path:
|
||||
continue
|
||||
try:
|
||||
mtimes.append(os.path.getmtime(os.path.join(static_root, rel_path)))
|
||||
except OSError:
|
||||
continue
|
||||
if not mtimes:
|
||||
return None
|
||||
return str(int(max(mtimes)))
|
||||
|
||||
|
||||
@pages_bp.route("/")
|
||||
def index():
|
||||
"""主页 - 重定向到登录或应用"""
|
||||
@@ -110,10 +132,18 @@ def admin_page():
|
||||
logger.warning(f"[admin_spa] manifest缺少入口文件: {manifest_path}")
|
||||
return render_template("admin_legacy.html")
|
||||
|
||||
admin_spa_js_file = f"admin/{js_file}"
|
||||
admin_spa_css_files = [f"admin/{p}" for p in css_files]
|
||||
admin_spa_build_id = _get_asset_build_id(
|
||||
os.path.join(current_app.root_path, "static"),
|
||||
[admin_spa_js_file, *admin_spa_css_files],
|
||||
)
|
||||
|
||||
return render_template(
|
||||
"admin.html",
|
||||
admin_spa_js_file=f"admin/{js_file}",
|
||||
admin_spa_css_files=[f"admin/{p}" for p in css_files],
|
||||
admin_spa_js_file=admin_spa_js_file,
|
||||
admin_spa_css_files=admin_spa_css_files,
|
||||
admin_spa_build_id=admin_spa_build_id,
|
||||
)
|
||||
except FileNotFoundError:
|
||||
logger.warning(f"[admin_spa] 未找到manifest: {manifest_path},回退旧版后台模板")
|
||||
|
||||
Reference in New Issue
Block a user