🐛 避免主题资源命中旧缓存
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
# @File : main.py
|
||||
# @Software: PyCharm
|
||||
import asyncio
|
||||
import re
|
||||
import time
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
@@ -147,6 +148,7 @@ app.include_router(presign_api, prefix="/api")
|
||||
app.include_router(admin_api)
|
||||
|
||||
|
||||
@app.get("/theme-assets/{asset_path:path}")
|
||||
@app.get("/assets/{asset_path:path}")
|
||||
async def theme_asset(asset_path: str):
|
||||
theme_assets = (BASE_DIR / settings.themesSelect / "assets").resolve()
|
||||
@@ -166,17 +168,26 @@ async def theme_asset(asset_path: str):
|
||||
@app.exception_handler(404)
|
||||
@app.get("/")
|
||||
async def index(request=None, exc=None):
|
||||
return HTMLResponse(
|
||||
content=open(
|
||||
BASE_DIR / f"{settings.themesSelect}/index.html", "r", encoding="utf-8"
|
||||
)
|
||||
theme_dir = BASE_DIR / settings.themesSelect
|
||||
asset_version = str(int((theme_dir / "index.html").stat().st_mtime))
|
||||
content = (
|
||||
open(theme_dir / "index.html", "r", encoding="utf-8")
|
||||
.read()
|
||||
.replace("{{title}}", str(settings.name))
|
||||
.replace("{{description}}", str(settings.description))
|
||||
.replace("{{keywords}}", str(settings.keywords))
|
||||
.replace("{{opacity}}", str(settings.opacity))
|
||||
.replace('"/assets/', '"assets/')
|
||||
.replace("{{background}}", str(settings.background)),
|
||||
.replace('"/assets/', '"/theme-assets/')
|
||||
.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",
|
||||
headers={"Cache-Control": "no-cache"},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user