Typesafe Frontend Routing in Rust/Leptos

submited by
Style Pass
2025-01-20 17:30:19

I prototyped a type-safe routing solution for leptos, the full-stack Rust web framework. The approach comprises:a DSL for defining routes parsed with a macro,magic handler functions,a macro to construct typesafe URLs.

If a better alternative for typed routing wasn't available, I personally would think the benefits of type safety outweigh the downsides of the approach (ie, consequences of macro and trait magic). I'm keen to see how leptos-routable will shape up, because it also takes aim at type-safe routing in leptos. And I suspect it will end up as the better approach.

In a web app, one usually has a bunch of routes (let's use react / react-router as an accessible example):<Routes> <Route path="dashboard" element={<Dashboard />}> <Route index element={<Home />} /> <Route path="teams/:teamId" element={<Team />} /> </Route> </Routes>

And one links to them elsewhere in the app:<Link to={`/dashboard/teams/${myTeamId}`}>Dashboard</Link>

Leave a Comment