fix(doc): 修复 /doc 路由与 Dioxus 静态托管冲突的 panic

Dioxus 在 dev 用 nest_service("/doc", ServeDir) 托管 public/doc/ 目录,
手动在 static_routes 注册 /doc 会在 merge 时触发路由冲突 panic。

根因(dioxus-server serve_dir_cached):dev 模式对 public/ 下的目录用
nest_service 挂整个 ServeDir,注册了 /doc;我在 static_routes 加的
精确 /doc 路由与之同名冲突。

修复:
- main.rs: 移除手动注册的 /doc 与 /doc/ 重定向路由,交给 Dioxus 托管。
  文档深层路径 /doc/yggdrasil/* 由 ServeDir 直接服务。
- Makefile: doc 目标拷贝后额外生成 public/doc/index.html 重定向页,
  ServeDir 访问目录根时返回它,经 meta refresh + JS 跳转到
  yggdrasil/index.html,让裸路径 /doc 也能直达文档。
- build/clean 同步集成 make doc 与清理 public/doc。
This commit is contained in:
xfy 2026-06-26 14:27:21 +08:00
parent 30a861f910
commit 1e78c7f0f9
2 changed files with 15 additions and 17 deletions

View File

@ -6,6 +6,7 @@ build:
@$(MAKE) build-core @$(MAKE) build-core
@$(MAKE) highlight-css @$(MAKE) highlight-css
@tailwindcss -i input.css -o public/style.css --minify @tailwindcss -i input.css -o public/style.css --minify
@$(MAKE) doc
@dx build --release --debug-symbols=false @dx build --release --debug-symbols=false
build-linux: build-linux:
@ -75,8 +76,21 @@ test:
# RUSTDOCFLAGS 把 rustdoc 的 --default-theme=ayu 透传过去——cargo doc 本身 # RUSTDOCFLAGS 把 rustdoc 的 --default-theme=ayu 透传过去——cargo doc 本身
# 无主题参数,但会把该环境变量转交给底层 rustdoc。注意它是默认值浏览器 # 无主题参数,但会把该环境变量转交给底层 rustdoc。注意它是默认值浏览器
# 若已记住上次的主题选择localStorage则不会被覆盖。 # 若已记住上次的主题选择localStorage则不会被覆盖。
#
# 生成后拷贝到 public/doc/,让文档随 Dioxus 静态目录发布。先清空旧目录再
# 整体拷贝避免删除模块后残留旧文件。rustdoc 内部用相对路径引用资源
# (如 ../../static.files/),原样挂载不会断链。
#
# 额外生成 public/doc/index.html 重定向页Dioxus 在 dev 用
# nest_service("/doc", ServeDir) 托管该目录ServeDir 访问目录根时默认
# 返回 index.html。用 meta refresh + JS 跳转到真正的文档入口
# yggdrasil/index.html这样裸路径 /doc 也能直达文档,且不与 Dioxus 的
# /doc/* 路由冲突(手动注册 /doc 会在 merge 时 panic
doc: doc:
@RUSTDOCFLAGS="--default-theme=ayu" cargo doc --no-deps --document-private-items @RUSTDOCFLAGS="--default-theme=ayu" cargo doc --no-deps --document-private-items
@rm -rf public/doc
@cp -r target/doc public/doc
@printf '<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="refresh" content="0;url=yggdrasil/index.html"><title>Redirecting…</title></head><body><script>location.replace("yggdrasil/index.html")</script></body></html>' > public/doc/index.html
# 同 doc生成完自动用浏览器打开。 # 同 doc生成完自动用浏览器打开。
doc-open: doc-open:
@ -85,4 +99,5 @@ doc-open:
clean: clean:
@cargo clean @cargo clean
@rm -f public/style.css public/highlight.css @rm -f public/style.css public/highlight.css
@rm -rf public/doc
@rm -rf uploads/.cache @rm -rf uploads/.cache

View File

@ -391,23 +391,6 @@ fn main() {
"/readyz", "/readyz",
axum::routing::get(crate::api::health::readyz), axum::routing::get(crate::api::health::readyz),
) )
// /doc 与 /doc/ 重定向到文档入口 /doc/yggdrasil/index.html。
// public/doc/ 由 Dioxus 自动托管,深层路径无需额外处理;仅裸路径需要
// 重定向,否则会落到 Router 的 /:..segments 兜底返回 404 页面。
// 放在 static_routes与 /uploads 同层),不经过 CSRF/超时/缓存中间件,
// 行为与静态资源一致。用 301 永久重定向,让浏览器/书签记住跳转。
.route(
"/doc",
axum::routing::get(|| async {
axum::response::Redirect::permanent("/doc/yggdrasil/index.html")
}),
)
.route(
"/doc/",
axum::routing::get(|| async {
axum::response::Redirect::permanent("/doc/yggdrasil/index.html")
}),
)
.route( .route(
"/uploads/{*path}", "/uploads/{*path}",
axum::routing::get(crate::api::image::serve_image), axum::routing::get(crate::api::image::serve_image),