Compare commits

..

No commits in common. "2b048b94a3190a1bc30306cd576a595691edad4a" and "f9cf1f71e4c4c100029ad36e73967fbcc41942a5" have entirely different histories.

3 changed files with 9 additions and 58 deletions

View File

@ -6,24 +6,13 @@
//! 该页面为静态展示页面,不发起任何 server function 调用。
use dioxus::prelude::*;
use dioxus::router::components::Link;
use crate::router::Route;
/// 404 页面组件,对应兜底路由 `/:..segments`。
///
/// 展示大号的装饰性 404 数字、状态标签、错误说明以及返回首页的链接。
///
/// # 两种命中路径
/// 本组件在两种完全不同的机制下被渲染:
/// 1. **路由匹配** —— 访问任意未命中路径Router 命中 catch-all `Route::NotFound`
/// 此时 `ErrorBoundary` **无错误**,本组件作为 childrenOutlet正常渲染。
/// 2. **错误冒泡** —— 如 `PostDetail` 对不存在的 slug 抛出 `ServerFnError(404)`
/// `ErrorLayout` 的 `ErrorBoundary` 捕获后在 fallback 里渲染本组件。
///
/// 「返回首页」必须在导航前清除可能存在的错误边界,否则场景 2 会卡死:
/// ErrorBoundary 持有错误时不渲染 childrenOutlet路由虽切到 `Home`
/// 页面仍停留在 fallback本组件表现为「URL 变了但页面不变」。
/// 场景 1 下没有错误,`clear_errors` 是 no-op。
#[component]
pub fn NotFound(segments: Vec<String>) -> Element {
let _ = segments;
@ -62,19 +51,10 @@ pub fn NotFound(segments: Vec<String>) -> Element {
"这个页面似乎走丢了,或者从未存在过。"
}
// 返回首页:用 onclick 先清除错误边界再导航。
// 直接用 Link 无法干预点击时机,故改为按钮 + 命令式导航。
// 详见组件顶部文档:场景 2错误冒泡下若不清除错误
// ErrorBoundary 会一直渲染 fallback路由切换后页面仍卡在本页。
button {
r#type: "button",
onclick: move |_| {
if let Some(ctx) = try_consume_context::<ErrorContext>() {
ctx.clear_errors();
}
let _ = dioxus::router::navigator().push(Route::Home {});
},
class: "group inline-flex items-center gap-2 px-5 py-2.5 text-sm font-medium text-paper-primary bg-paper-entry border border-paper-border rounded-lg hover:border-paper-secondary hover:bg-paper-border transition-all cursor-pointer",
// 返回首页
Link {
to: Route::Home {},
class: "group inline-flex items-center gap-2 px-5 py-2.5 text-sm font-medium text-paper-primary bg-paper-entry border border-paper-border rounded-lg hover:border-paper-secondary hover:bg-paper-border transition-all",
svg {
xmlns: "http://www.w3.org/2000/svg",
width: "16",

View File

@ -129,11 +129,6 @@ fn ErrorLayout() -> Element {
rsx! {
ErrorBoundary {
handle_error: move |err: ErrorContext| {
// 克隆一份错误边界句柄,供 fallback 内的「返回首页」按钮清除错误。
// ErrorContext 内部是 Rc<RefCell<...>>clone 廉价。
// 不清除就导航会卡死ErrorBoundary 持有错误时永远渲染 fallback
// 不渲染 childrenOutlet导致 URL 变了但页面不变。
let err_ctx = err.clone();
// Commit the status code on the server side
#[cfg(feature = "server")]
{
@ -207,18 +202,9 @@ fn ErrorLayout() -> Element {
"抱歉,加载页面时出现了一些错误,请稍后再试。"
}
// CTA: 清除错误边界后命令式导航回首页。
// 必须用按钮而非 Link —— Link 无法在导航前清除错误,
// 会导致 ErrorBoundary 卡在 fallback页面不随路由切换更新。
button {
r#type: "button",
onclick: {
let err_ctx = err_ctx.clone();
move |_| {
err_ctx.clear_errors();
let _ = dioxus::router::navigator().push(Route::Home {});
}
},
// CTA: Link back to home, styled with subtle border & background transition
dioxus::router::components::Link {
to: Route::Home {},
class: "group inline-flex items-center gap-2 px-6 py-2.5 text-sm font-medium text-paper-primary bg-paper-theme border border-paper-border rounded-full hover:border-paper-secondary hover:bg-paper-border transition-all duration-200 cursor-pointer shadow-sm active:scale-[0.98]",
svg {
xmlns: "http://www.w3.org/2000/svg",

View File

@ -35,26 +35,11 @@ pub async fn sleep_ms(ms: u32) {
}
/// 异步睡眠指定毫秒数(原生 tokio 版本)。
///
/// 仅在 `server` feature 启用且非 wasm32 目标下编译。`tokio` 是 server-only 的
/// optional 依赖(见 Cargo.toml不可用 `#[cfg(not(target_arch = "wasm32"))]`——
/// 那样会在「非 wasm32 主机 + 仅 web feature」组合下误激活此时 tokio 未引入,
/// 导致编译失败(此 bug 曾被 `[dev-dependencies] tokio` 掩盖,发布构建才暴露)。
#[cfg(all(feature = "server", not(target_arch = "wasm32")))]
#[cfg(not(target_arch = "wasm32"))]
pub async fn sleep_ms(ms: u32) {
tokio::time::sleep(std::time::Duration::from_millis(ms as u64)).await;
}
/// `sleep_ms` 的占位 stub仅用于「非 wasm32 且非 server」的无效构建组合。
///
/// 此组合(如非 wasm32 主机执行 `cargo build --features web`)不是有效部署目标——
/// web feature 的真实构建目标就是 wasm32会走上面的 JS setTimeout 分支。
/// 此 stub 仅保证符号可编译,永远不会在有效运行时被调用;若被调用说明部署配置错误。
#[cfg(all(not(feature = "server"), not(target_arch = "wasm32")))]
pub async fn sleep_ms(_ms: u32) {
panic!("sleep_ms 在非 wasm32 且非 server 的无效构建组合下被调用:请检查 feature 配置");
}
/// 获取当前时间戳(毫秒)。
///
/// WASM 端使用 `js_sys::Date::now()`,服务端回退到 `chrono::Utc`。