You build a small routed application in this tutorial: a root layout, an index page, a user detail page with a loader, and typed navigation between them. The same route table you create here runs on GPUI, Dioxus, Leptos, Xilem, and Floem — only the three-line host bootstrap differs.
Prerequisites
Section titled “Prerequisites”- Rust 1.85 or newer (Navi uses edition 2024).
- A working GPUI toolchain for your platform if you follow the GPUI host step.
- A clone of the navi workspace next to your project, because the quoin and rs-query crates it builds on are consumed as local path dependencies (see Installation).
1. Add the dependencies
Section titled “1. Add the dependencies”Add the Navi crates plus quoin with your framework’s feature to Cargo.toml:
[dependencies]navi-router = { path = "../navi/navi-router" }navi-macros = { path = "../navi/navi-macros" }quoin = { path = "../quoin/quoin", features = ["gpui"] }The features value selects your UI framework: gpui, dioxus, leptos, xilem, or floem. Everything else in this tutorial stays identical across the five.
2. Create the route files
Section titled “2. Create the route files”Routes are plain Rust files under src/routes. Create a root layout at src/routes/__root.rs — files named __root or starting with _ become layouts that render their children through an <Outlet>:
use navi_macros::define_route;use quoin_ui::{Node, StyleCmd};
fn root_node() -> Node { Node::div() .styles([StyleCmd::FlexCol]) .child(Link::new("/").child("Home").build()) .child(Outlet::new().build()) .build()}
Create the index page at src/routes/index.rs:
use navi_macros::define_route;use quoin_ui::{Node, StyleCmd};
fn index_node() -> Node { Node::div() .styles([StyleCmd::FlexCol, StyleCmd::Gap(6.0), StyleCmd::Padding(12.0)]) .child(Node::text("Welcome!")) .child(Link::new("/users/42").child("Open /users/42").build()) .build()}
Add the dynamic user route at src/routes/users/$id.rs. The $id file name declares a path parameter, and the loader runs when someone navigates to the route:
use std::sync::Arc;
use navi_macros::{define_route, use_loader_data};use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Serialize)]pub struct UserParams { pub id: String,}
#[derive(Clone, Debug, PartialEq)]pub struct UserData { pub id: String, pub name: String, pub email: String,}
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>>(Arc::new(UserData { id: id.clone(), name: format!("User {id}"), email: format!("user{id}@example.com"), })) }, node: user_node,);
fn user_node() -> Node { // Reads the data the loader returned for this route. let data = use_loader_data!(UsersParamIdRoute); match data.map(|d| (*d).clone()) { Some(user) => Node::text(format!("{} — {}", user.name, user.email)), None => Node::text("Loading..."), }}3. Configure code generation
Section titled “3. Configure code generation”Navi generates the route tree from your src/routes directory at build time. Create navi.config.json in your project root:
{ "routes_directory": "./src/routes", "generated_route_tree": "./src/route_tree.gen.rs", "route_token": "route", "index_token": "index", "route_file_ignore_prefix": "-"}Then wire the generator into build.rs:
fn main() { let config = navi_codegen::NaviConfig::from_file("navi.config.json") .expect("Failed to read config"); navi_codegen::generator::write_route_tree(&config).unwrap();}Every build now regenerates src/route_tree.gen.rs, which exposes build_route_tree() and register_routes().
4. Boot the app
Section titled “4. Boot the app”On GPUI, Xilem, and Floem you boot through quoin’s run_app! host macro. Initialize the router inside your host with the generated tree:
mod route_tree { include!("route_tree.gen.rs");}
let tree = route_tree::build_route_tree();let router = RouterProvider::new_with_options( window_id, window_handle, Location::new("/"), tree, RouterOptions { not_found_mode: NotFoundMode::Fuzzy, ..Default::default() }, cx,);route_tree::register_routes(cx);For the other frameworks only the bootstrap changes — the route table, loaders, guards, and devtools do not:
// Dioxus desktopfn app() -> Element { rsx! { NaviDemo {} } }fn main() { dioxus::LaunchBuilder::desktop().launch(app); }// Leptos CSR (wasm32)#[wasm_bindgen(start)]pub fn main() { leptos::mount::mount_to_body(|| view! { <NaviDemo /> });}5. Run it
Section titled “5. Run it”cargo runYou start on /, follow the link to /users/42, and watch the loader resolve into rendered user data. Static segments always win over dynamic ones during matching, so adding src/routes/users/new.rs later shadows $id for that exact path deterministically.
