You guard a route with a before_load hook. Navi runs it before the route loads, and the hook decides whether navigation proceeds, redirects elsewhere, or resolves as not-found.
Return one of three outcomes
Section titled “Return one of three outcomes”The hook receives a BeforeLoadContext and returns a BeforeLoadResult:
| Result | Effect |
|---|---|
BeforeLoadResult::Ok |
Navigation continues. |
BeforeLoadResult::Redirect(Redirect) |
Navi follows the redirect target instead. |
BeforeLoadResult::NotFound(NotFound) |
Navi resolves the navigation as not-found. |
The redirect() and not_found() helper functions construct the non-Ok variants, re-exported from navi_router:
use navi_router::{BeforeLoadResult, redirect, not_found};
AdminDashboardRoute, path: "/admin/dashboard", before_load: |ctx| async move { if !is_signed_in(&ctx) { return BeforeLoadResult::Redirect(redirect("/login")); } BeforeLoadResult::Ok }, node: admin_node,);React to route lifecycle changes
Section titled “React to route lifecycle changes”Routes also accept two synchronous lifecycle callbacks, each receiving the Location:
| Argument | Fires |
|---|---|
on_enter |
When someone enters the route. |
on_leave |
When someone leaves the route. |
ProfileRoute, path: "/profile", on_enter: |loc| log::info!("entered {}", loc.pathname), on_leave: |loc| log::info!("left {}", loc.pathname), node: profile_node,);Order of operations
Section titled “Order of operations”A navigation flows through BeforeNavigate → BeforeLoad → Resolved events. The before_load hook sits between the navigation decision and data resolution, which is why a guard can redirect before any loader runs.
Next steps
Section titled “Next steps”Loaders & SuspenseWhat runs after the guard allows the navigation.
Core Types referenceBeforeLoadContext, BeforeLoadResult, Redirect, and NotFound.
