Skip to content

You attach a loader to any route and Navi runs it when someone navigates to that route, caches the result through rs-query, and hands the data to your UI.

The loader receives the route’s typed params and returns the type declared as data. The example below comes from the repository’s own example-app:

UsersParamIdRoute,
path: "/users/$id",
params: UserParams,
data: UserData,
loader: |params: UserParams| async move {
let id = params.id;
navi_router::sleep(std::time::Duration::from_millis(800)).await;
Ok::<_, Box<dyn std::error::Error + Send + Sync>>(std::sync::Arc::new(UserData {
id: id.clone(),
name: format!("User {id}"),
email: format!("user{id}@example.com"),
}))
},
component: UserDetailPage,
);

Two define_route! arguments tune the cache behavior of a route’s data:

Argument Meaning
stale_time How long loaded data stays fresh, e.g. Duration::from_secs(60).
gc_time How long unused data survives in the cache, e.g. Duration::from_secs(300).

The cache is a real query cache — the devtools Cache tab lets you inspect and invalidate entries at runtime (see Devtools).

Awaited is the suspense component for route data: it renders a fallback while the loader runs and calls your closure with the data once resolved. This is the pattern the repository’s awaited.rs example uses:

awaited.rs
use navi_router::components::Awaited;
Awaited::<AwaitedDemoRoute>::new()
.fallback(|| quoin_ui::Node::div().child("Loading...").build())
.child(|data: String| {
quoin_ui::Node::div().child(format!("Data: {data}")).build()
})
.build()

The .child closure receives the loaded data value.

Wrap loading regions with SuspenseBoundary

Section titled “Wrap loading regions with SuspenseBoundary”

SuspenseBoundary displays a fallback UI while nested routes load. It takes a fallback closure and renders the matched child route inside once loading completes:

use navi_router::components::SuspenseBoundary;
SuspenseBoundary::new(|| quoin_ui::Node::text("Loading section…").build()).build()

Place it in a layout to give an entire subtree a shared loading state.