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}")
|
logger.warning(f"[app_spa] manifest缺少入口文件: {manifest_path}")
|
||||||
return render_template(legacy_template_name, **legacy_context)
|
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(
|
return render_template(
|
||||||
"app.html",
|
"app.html",
|
||||||
app_spa_js_file=f"app/{js_file}",
|
app_spa_js_file=app_spa_js_file,
|
||||||
app_spa_css_files=[f"app/{p}" for p in css_files],
|
app_spa_css_files=app_spa_css_files,
|
||||||
|
app_spa_build_id=app_spa_build_id,
|
||||||
app_spa_initial_state=spa_initial_state,
|
app_spa_initial_state=spa_initial_state,
|
||||||
)
|
)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
@@ -50,6 +58,20 @@ def render_app_spa_or_legacy(
|
|||||||
return render_template(legacy_template_name, **legacy_context)
|
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("/")
|
@pages_bp.route("/")
|
||||||
def index():
|
def index():
|
||||||
"""主页 - 重定向到登录或应用"""
|
"""主页 - 重定向到登录或应用"""
|
||||||
@@ -110,10 +132,18 @@ def admin_page():
|
|||||||
logger.warning(f"[admin_spa] manifest缺少入口文件: {manifest_path}")
|
logger.warning(f"[admin_spa] manifest缺少入口文件: {manifest_path}")
|
||||||
return render_template("admin_legacy.html")
|
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(
|
return render_template(
|
||||||
"admin.html",
|
"admin.html",
|
||||||
admin_spa_js_file=f"admin/{js_file}",
|
admin_spa_js_file=admin_spa_js_file,
|
||||||
admin_spa_css_files=[f"admin/{p}" for p in css_files],
|
admin_spa_css_files=admin_spa_css_files,
|
||||||
|
admin_spa_build_id=admin_spa_build_id,
|
||||||
)
|
)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
logger.warning(f"[admin_spa] 未找到manifest: {manifest_path},回退旧版后台模板")
|
logger.warning(f"[admin_spa] 未找到manifest: {manifest_path},回退旧版后台模板")
|
||||||
|
|||||||
@@ -5,13 +5,20 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
|
||||||
<title>后台管理 - 知识管理平台</title>
|
<title>后台管理 - 知识管理平台</title>
|
||||||
{% for css_file in admin_spa_css_files %}
|
{% for css_file in admin_spa_css_files %}
|
||||||
|
{% if admin_spa_build_id %}
|
||||||
|
<link rel="stylesheet" href="{{ url_for('serve_static', filename=css_file, v=admin_spa_build_id) }}" />
|
||||||
|
{% else %}
|
||||||
<link rel="stylesheet" href="{{ url_for('serve_static', filename=css_file) }}" />
|
<link rel="stylesheet" href="{{ url_for('serve_static', filename=css_file) }}" />
|
||||||
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<noscript>该页面需要启用 JavaScript 才能使用。</noscript>
|
<noscript>该页面需要启用 JavaScript 才能使用。</noscript>
|
||||||
<div id="app"></div>
|
<div id="app"></div>
|
||||||
|
{% if admin_spa_build_id %}
|
||||||
|
<script type="module" src="{{ url_for('serve_static', filename=admin_spa_js_file, v=admin_spa_build_id) }}"></script>
|
||||||
|
{% else %}
|
||||||
<script type="module" src="{{ url_for('serve_static', filename=admin_spa_js_file) }}"></script>
|
<script type="module" src="{{ url_for('serve_static', filename=admin_spa_js_file) }}"></script>
|
||||||
|
{% endif %}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,11 @@
|
|||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
|
||||||
<title>知识管理平台</title>
|
<title>知识管理平台</title>
|
||||||
{% for css_file in app_spa_css_files %}
|
{% for css_file in app_spa_css_files %}
|
||||||
|
{% if app_spa_build_id %}
|
||||||
|
<link rel="stylesheet" href="{{ url_for('serve_static', filename=css_file, v=app_spa_build_id) }}" />
|
||||||
|
{% else %}
|
||||||
<link rel="stylesheet" href="{{ url_for('serve_static', filename=css_file) }}" />
|
<link rel="stylesheet" href="{{ url_for('serve_static', filename=css_file) }}" />
|
||||||
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -16,6 +20,10 @@
|
|||||||
window.__APP_INITIAL_STATE__ = {{ app_spa_initial_state | tojson }};
|
window.__APP_INITIAL_STATE__ = {{ app_spa_initial_state | tojson }};
|
||||||
</script>
|
</script>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
{% if app_spa_build_id %}
|
||||||
|
<script type="module" src="{{ url_for('serve_static', filename=app_spa_js_file, v=app_spa_build_id) }}"></script>
|
||||||
|
{% else %}
|
||||||
<script type="module" src="{{ url_for('serve_static', filename=app_spa_js_file) }}"></script>
|
<script type="module" src="{{ url_for('serve_static', filename=app_spa_js_file) }}"></script>
|
||||||
|
{% endif %}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user