🐛 避免主题资源命中旧缓存
Build and push Docker image / buildx (push) Has been cancelled
Deploy VitePress site to Pages / build (push) Has been cancelled
Deploy VitePress site to Pages / Deploy (push) Has been cancelled

This commit is contained in:
2026-06-05 17:47:56 +08:00
parent 4953b6a68d
commit 7bba4313bd
+17 -6
View File
@@ -3,6 +3,7 @@
# @File : main.py # @File : main.py
# @Software: PyCharm # @Software: PyCharm
import asyncio import asyncio
import re
import time import time
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
@@ -147,6 +148,7 @@ app.include_router(presign_api, prefix="/api")
app.include_router(admin_api) app.include_router(admin_api)
@app.get("/theme-assets/{asset_path:path}")
@app.get("/assets/{asset_path:path}") @app.get("/assets/{asset_path:path}")
async def theme_asset(asset_path: str): async def theme_asset(asset_path: str):
theme_assets = (BASE_DIR / settings.themesSelect / "assets").resolve() theme_assets = (BASE_DIR / settings.themesSelect / "assets").resolve()
@@ -166,17 +168,26 @@ async def theme_asset(asset_path: str):
@app.exception_handler(404) @app.exception_handler(404)
@app.get("/") @app.get("/")
async def index(request=None, exc=None): async def index(request=None, exc=None):
return HTMLResponse( theme_dir = BASE_DIR / settings.themesSelect
content=open( asset_version = str(int((theme_dir / "index.html").stat().st_mtime))
BASE_DIR / f"{settings.themesSelect}/index.html", "r", encoding="utf-8" content = (
) open(theme_dir / "index.html", "r", encoding="utf-8")
.read() .read()
.replace("{{title}}", str(settings.name)) .replace("{{title}}", str(settings.name))
.replace("{{description}}", str(settings.description)) .replace("{{description}}", str(settings.description))
.replace("{{keywords}}", str(settings.keywords)) .replace("{{keywords}}", str(settings.keywords))
.replace("{{opacity}}", str(settings.opacity)) .replace("{{opacity}}", str(settings.opacity))
.replace('"/assets/', '"assets/') .replace('"/assets/', '"/theme-assets/')
.replace("{{background}}", str(settings.background)), .replace('"assets/', '"/theme-assets/')
.replace("{{background}}", str(settings.background))
)
content = re.sub(
r'((?:theme-)?assets/[^"\']+\.(?:css|js|json|png))(["\'])',
rf"\1?v={asset_version}\2",
content,
)
return HTMLResponse(
content=content,
media_type="text/html", media_type="text/html",
headers={"Cache-Control": "no-cache"}, headers={"Cache-Control": "no-cache"},
) )