Archived
为什么:React 迁移已验证完毕(浏览器冒烟全过、golden 刷新), legacy 模板兜底分支指向已删除的文件只会制造幻觉路径。删除 templates/index.html(466KB)与 static/site_auth.js(80KB)及 /static 挂载;jinja2 随兜底分支失去唯一使用者,连同 markupsafe 一并移出依赖(63 包 -2)。_render_index 简化为直接读构建产物—— dist 已入库恒存在,无需分支。
31 lines
1006 B
Python
31 lines
1006 B
Python
# -*- coding: utf-8 -*-
|
||
"""首页。
|
||
|
||
- GET / 首页:服务 React 构建产物 frontend/dist/index.html
|
||
(vite 固定产物文件名,字节级稳定,golden 契约依赖此点)
|
||
- 构建产物经 /assets 挂载(见 web/app.py)
|
||
"""
|
||
from pathlib import Path
|
||
|
||
from fastapi import APIRouter, Request
|
||
from fastapi.responses import HTMLResponse
|
||
|
||
router = APIRouter()
|
||
|
||
_PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent
|
||
_DIST_INDEX = _PROJECT_ROOT / 'frontend' / 'dist' / 'index.html'
|
||
|
||
|
||
def _render_index() -> str:
|
||
"""返回 React 构建产物原样字节(构建产物入库,恒存在)。"""
|
||
return _DIST_INDEX.read_text(encoding='utf-8')
|
||
|
||
|
||
@router.get('/', response_class=HTMLResponse)
|
||
async def index(request: Request) -> HTMLResponse:
|
||
"""首页(禁止缓存,确保前端更新即时生效)。"""
|
||
return HTMLResponse(
|
||
content=_render_index(),
|
||
headers={'Cache-Control': 'no-cache, no-store, must-revalidate'},
|
||
)
|