You've already forked agentic-coding-workflow
前端页面调整
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
// Package httpx 内的 spa.go 提供 SPA 静态资源 + 404 fallback。
|
||||
// 命中文件直接 ServeFile;不存在但路径无后缀视为前端路由,回退 index.html;
|
||||
// 命中文件直接 ServeFile;不存在且最后一段无扩展名视为前端路由,回退 index.html;
|
||||
// /api/ 前缀始终返回 404(避免把 API 拼写错误掩盖成 HTML)。
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
stdpath "path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -18,16 +18,18 @@ func NewSPAHandler(distFS fs.FS) http.Handler {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
path := strings.TrimPrefix(r.URL.Path, "/")
|
||||
if path == "" {
|
||||
path = "index.html"
|
||||
// Trim 两侧斜杠:带尾斜杠的路径(如 /p/x/w/)对 fs.Stat 是非法 fs 路径,
|
||||
// 返回 ErrInvalid 而非 ErrNotExist,必须先归一化才能正确触发回退。
|
||||
name := strings.Trim(r.URL.Path, "/")
|
||||
if name == "" {
|
||||
name = "index.html"
|
||||
}
|
||||
if _, err := fs.Stat(distFS, path); err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) && !strings.Contains(path, ".") {
|
||||
r.URL.Path = "/index.html"
|
||||
fileServer.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
if _, err := fs.Stat(distFS, name); err != nil && stdpath.Ext(name) == "" {
|
||||
// 前端路由:直接发 index.html 内容,不能改写 r.URL.Path 再走
|
||||
// fileServer——FileServer 会把 /index.html 301 重定向到 "./",
|
||||
// 深层路由刷新会被改写成带尾斜杠的错误 URL。
|
||||
http.ServeFileFS(w, r, distFS, "index.html")
|
||||
return
|
||||
}
|
||||
fileServer.ServeHTTP(w, r)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user