Tiny router for Svelte 5 SPAs. Runes friendly.
- Minimal, fast, and dependency-free
- Supports dynamic segments (e.g.
/user/:id) - Fallback route support
- Route param and query param helpers
- Imperative navigation API
npm install @ryuz/rsv<script lang="ts">
import { Router, Route, navigate, useRsv } from '@ryuz/rsv';
</script>
<Router>
<Route path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/user/:id" component={User} />
<Route><!-- fallback route, rendered if no other matches --></Route>
</Router>RSV supports both history and hash-based routing. Hash mode is useful for SPAs that are served from static file hosts or environments where server-side routing is not available.
Set the mode prop on the Router component:
<Router mode="hash">
<Route path="/foo" component={Foo} />
<Route path="/bar" component={Bar} />
</Router>When using hash mode, anchor tags should use href="#/your-path" to ensure navigation works as expected:
<a href="#/bar">Go to Bar</a>Imperative navigation (router.navigate('/bar') or navigate('/bar')) works the same in both modes.
- Deploying to static file hosts (e.g., GitHub Pages, S3, Netlify static)
- No server-side support for SPA routing
- Wanting URLs like
/#/fooinstead of/foo
If a route contains :param, the matched value is passed as a prop:
<Route path="/user/:id" component={User} />
<!-- User receives prop { id } -->If you omit the path prop, the Route acts as a fallback:
<Route><!-- This renders if no other Route matches --></Route>import { navigate } from '@ryuz/rsv';
navigate('/about');
navigate('/login', { replace: true });import { useRsv } from '@ryuz/rsv';
const router = useRsv();
const currentPath = $derived(router.path);router.getQueryParam(key)– get a query param valuerouter.hasQueryParam(key)– check if param existsrouter.removeQueryParams([keys])– remove query params from URLrouter.getParam(key)– get a dynamic route param value (e.g.idfrom/user/:id)
- Provides routing context and manages URL state for child
<Route>components. - Props:
url(optional): initial path for SSR/testingchildren: Route components (via slot)
- Renders its content when the current path matches
path(supports dynamic segments, e.g./user/:id). - Props:
path(optional): route pattern. If omitted, acts as fallback route.component(optional): Svelte component to renderchildren: fallback content if nocomponentprop
- Dynamic segments (e.g.
:id) are passed as props to the rendered component and are also accessible viarouter.getParam(key).
- Programmatically change the route.
replace: if true, replaces history entry
- Returns the router context object:
path: current pathroutes: all registered route patternsquery: query params as objectparams: dynamic params as objectnavigate(to, options): navigation functiongetQueryParam(key): get query param valuehasQueryParam(key): check if query param existsremoveQueryParams([keys]): remove query params from URLgetParam(key): get dynamic route param value
Prior art - svelte-tiny-router