Router
Router matches the current path, such as /inbox or /projects/42, and
returns the screen for that route. It is Fission's default matcher, not a
required navigation authority; apps and libraries can consume
ShellRouteChanged and handle routing themselves.
That design is important for cross-platform work. Link, NavigationCommand, RouteLocation, and ShellRouteChanged are shell-neutral. The Web shell maps them to pathname or hash URLs and browser history; graphical and Terminal shells maintain their own logical history. The Router only matches the resulting string and builds the corresponding screen.
Example
use fission::prelude::*;
fission_component! {
pub AppRoutes<AppState>
view(_routes, state, _cx) =>
Router::<AppState>::new()
.with_path(state.current_path.clone())
.route_component("/projects", ProjectList)
.route_component("/settings", SettingsScreen)
.route_component("/projects/:project_id", ProjectRoute)
}
route_component stores a concrete component and converts it only after its
path matches. The existing closure-based route and parameter-aware
route_builder APIs remain available.
Captured parameters are available to every registration form. A
route_builder receives &RouteParams directly. A named component registered
with route_component reads the same map from its matched build scope:
fission_component! {
pub ProjectRoute<AppState>
view(_route, _state, _cx) {
let params = fission::build::read::<RouteParams>();
let project_id = params.get("project_id").cloned();
} =>
ProjectScreen(project_id)
}
ProjectScreen declares a project_id: Option<String> prop and handles an
absent or invalid identifier explicitly. The router extracts strings; it does
not turn a path parameter into a validated database identifier.
The provider exists only while the matched component is converted. Nested
components therefore see the correct values, and unmatched routes cannot read
stale parameters from another route.
Protected routes
Keep access checks at the route table rather than repeating them inside every
protected screen. Derive a RouteDecision from application state and wrap the
route component with ProtectedRoute:
let account_access = match &view.state().session {
SessionState::Loading => RouteDecision::Pending,
SessionState::Authenticated(_) => RouteDecision::Allow,
SessionState::SignedOut => RouteRedirect::replace("/sign-in")
.return_to(&view.env().current_route)
.into(),
SessionState::Failed(_) => RouteDecision::Deny,
};
let router: Widget = Router::<AppState>::new()
.with_path(view.state().current_path.clone())
.route_component(
"/account",
ProtectedRoute::new(account_access, AccountRoute),
)
.route_component("/sign-in", SignInRoute)
.into();
ProtectedRoute converts only its selected component. While the decision is
pending, denied, or redirecting, AccountRoute cannot register reducers,
resources, jobs, or local state. Use .pending(...) and .denied(...) to
replace the default progress and access-denied branches.
Authentication restoration remains an ordinary asynchronous Fission effect.
For example, a startup reducer can read a session from the Store and update
the route decision in its success or failure reducer. See
ProtectedRoute for both the synchronous
selector and asynchronous SQLite patterns. Redirects replace history by default, preventing browser Back from immediately
retrying the rejected route. SSR returns an HTTP 302 without protected markup,
and denial returns HTTP 403. Static routes can emit a redirect document when
the decision is known at build time, but a static artifact cannot authenticate
a visitor. Route protection controls UI construction; APIs and server actions
must still enforce authorization independently.
Register the route reducer with .with_route_handler(...), then issue navigation from reducers with ctx.effects.navigate("/projects/42"), replace_route, navigation_back, navigation_forward, or navigation_go. A navigational Link::to(...) enters the same contract.
Field table
| | | |
|---|
| | The path string to match this frame. | Usually lives in GlobalState so reducers can update it explicitly. |
| | Ordered list of route patterns and lazily converted route components or page builders. | The router returns the first matching route. Order matters. |
| | Fallback builder when no route matches. | If omitted, the router renders a plain 404: {current_path} text node. |
Matching behavior
The current matcher is deliberately small:
/users/:id matches /users/123
/users/:id does not match /users/123/details
empty leading and trailing slashes are ignored during matching
parameter values are returned as strings in RouteParams This small contract makes routing easy to test. If a path renders the wrong screen, you only need to inspect the current path string and the ordered route list.
The active shell initializes Env.current_route and dispatches ShellRouteChanged before the first frame. Web additionally listens to popstate and hashchange. In WebRouteStrategy::Auto, an initial #/path keeps hash routing and an initial /path keeps pathname routing; applications can explicitly choose Path or Hash through WebApp::with_navigation(...).
The key point is that this contract does not require Router. A custom router can register the same route handler, read RouteLocation, issue the same navigation effects, and use SemanticsRegion::hyperlink(...) or Pressable::hyperlink(...) to produce genuine browser links.
Specific advice
Keep current_path normalized. Pick one format for trailing slashes, slug casing, and identifier encoding so reducers and tests do not have to guess what a valid path looks like.
Also keep route changes explicit. If a button means "go to /settings," dispatch a navigation action that updates state. Avoid scattering manual string mutations through unrelated reducers.
Check routing in context
Keep paths normalized so navigation actions and route matching use the same format.
Test direct loads and in-app navigation for the path or hash strategy your shell uses; verify Back and Forward if the target supports them.
Keep access decisions in the route boundary and enforce authorization again in protected server operations.