defineRoutes().
The pattern below still works perfectly — defineRoutes() is a recommended
addition on top, not a replacement.Installation
Install the package using your preferred package manager:
Requires Node.js 22+ for production builds (Node 20 has Svelte 5 compatibility issues). See What's New for the latest release highlights.
Basic Setup
Follow these three simple steps to get your router running:
1. Define Your Routes
Create a routes configuration object mapping paths to Svelte components:
2. Add the Router Component
Import and use the Router component in your main App component:
3. Create Navigation Links
Use the link action to make links work with the router:
Choose Your Routing Mode
The router supports two modes: Hash mode (default) and History mode.
Hash Mode (Default)
URLs: example.com/#/about
Advantages:
- No server configuration needed
- Works on static hosting
- Works with file:// protocol
Setup:
History Mode
URLs: example.com/about
Advantages:
- Clean URLs without #
- Better SEO
- Better user experience
Setup:
Your First Route Component
Route components are regular Svelte components. Here's a simple example:
Route Components with Parameters
Access route parameters via props:
What's Next?
Try the Live Examples
See the router in action — two live deploys, same source, different routing modes:
Running locally: make dev (port 5050, history) or make dev-hash (port 5051, hash). See Examples for the full per-feature deep-link list.