Compare commits
2 Commits
f9cf1f71e4
...
2b048b94a3
| Author | SHA1 | Date | |
|---|---|---|---|
| 2b048b94a3 | |||
| 5a10b07bff |
@ -6,13 +6,24 @@
|
||||
//! 该页面为静态展示页面,不发起任何 server function 调用。
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use dioxus::router::components::Link;
|
||||
|
||||
use crate::router::Route;
|
||||
|
||||
/// 404 页面组件,对应兜底路由 `/:..segments`。
|
||||
///
|
||||
/// 展示大号的装饰性 404 数字、状态标签、错误说明以及返回首页的链接。
|
||||
///
|
||||
/// # 两种命中路径
|
||||
/// 本组件在两种完全不同的机制下被渲染:
|
||||
/// 1. **路由匹配** —— 访问任意未命中路径,Router 命中 catch-all `Route::NotFound`,
|
||||
/// 此时 `ErrorBoundary` **无错误**,本组件作为 children(Outlet)正常渲染。
|
||||
/// 2. **错误冒泡** —— 如 `PostDetail` 对不存在的 slug 抛出 `ServerFnError(404)`,
|
||||
/// `ErrorLayout` 的 `ErrorBoundary` 捕获后在 fallback 里渲染本组件。
|
||||
///
|
||||
/// 「返回首页」必须在导航前清除可能存在的错误边界,否则场景 2 会卡死:
|
||||
/// ErrorBoundary 持有错误时不渲染 children(Outlet),路由虽切到 `Home`,
|
||||
/// 页面仍停留在 fallback(本组件),表现为「URL 变了但页面不变」。
|
||||
/// 场景 1 下没有错误,`clear_errors` 是 no-op。
|
||||
#[component]
|
||||
pub fn NotFound(segments: Vec<String>) -> Element {
|
||||
let _ = segments;
|
||||
@ -51,10 +62,19 @@ pub fn NotFound(segments: Vec<String>) -> Element {
|
||||
"这个页面似乎走丢了,或者从未存在过。"
|
||||
}
|
||||
|
||||
// 返回首页
|
||||
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",
|
||||
// 返回首页:用 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",
|
||||
svg {
|
||||
xmlns: "http://www.w3.org/2000/svg",
|
||||
width: "16",
|
||||
|
||||
@ -129,6 +129,11 @@ fn ErrorLayout() -> Element {
|
||||
rsx! {
|
||||
ErrorBoundary {
|
||||
handle_error: move |err: ErrorContext| {
|
||||
// 克隆一份错误边界句柄,供 fallback 内的「返回首页」按钮清除错误。
|
||||
// ErrorContext 内部是 Rc<RefCell<...>>,clone 廉价。
|
||||
// 不清除就导航会卡死:ErrorBoundary 持有错误时永远渲染 fallback,
|
||||
// 不渲染 children(Outlet),导致 URL 变了但页面不变。
|
||||
let err_ctx = err.clone();
|
||||
// Commit the status code on the server side
|
||||
#[cfg(feature = "server")]
|
||||
{
|
||||
@ -202,9 +207,18 @@ fn ErrorLayout() -> Element {
|
||||
"抱歉,加载页面时出现了一些错误,请稍后再试。"
|
||||
}
|
||||
|
||||
// CTA: Link back to home, styled with subtle border & background transition
|
||||
dioxus::router::components::Link {
|
||||
to: Route::Home {},
|
||||
// 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 {});
|
||||
}
|
||||
},
|
||||
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",
|
||||
|
||||
@ -35,11 +35,26 @@ pub async fn sleep_ms(ms: u32) {
|
||||
}
|
||||
|
||||
/// 异步睡眠指定毫秒数(原生 tokio 版本)。
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
///
|
||||
/// 仅在 `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")))]
|
||||
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`。
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user