Dynamic Routes

Route parameters allow you to create dynamic routes that match patterns instead of exact paths. This is essential for applications with user profiles, product pages, or any content identified by an ID or slug.

Named Parameters

Use :paramName syntax to define named parameters in your routes. Parameters are accessible in your component via props.

Basic Example

Route with single parameter
 
Accessing parameters in component
 

Multiple Parameters

You can have multiple parameters in a single route:

Multiple parameters
 
BlogPost.svelte
 

Optional Parameters

Add a ? after the parameter name to make it optional. Optional parameters can be omitted from the URL.

Optional parameters
 
Handling optional parameters
 

Wildcard Parameters

Use * to match any path segment or multiple segments. Wildcards are perfect for file browsers, nested paths, or catch-all routes.

Single Wildcard

Wildcard parameter
 
Using wildcard for file browser
 

Catch-All Route

Use a wildcard-only route as the last route to create a 404 page:

404 catch-all route
 

Combining Named Parameters and Wildcards

You can combine named parameters with wildcards for complex routing patterns:

Combined parameters
 
Using combined parameters
 

Accessing Parameters

There are multiple ways to access route parameters in your components:

1. Via Component Props (Recommended)

The simplest way - parameters are passed directly to your component:

Via props
 

2. Via routeParams() Function

Use the global routeParams() function for accessing parameters anywhere:

Via routeParams() function
 

TypeScript Support

Define types for your parameters to get full intellisense:

TypeScript parameters
 

Practical Examples

E-commerce Product Page

Product detail page
 

User Dashboard with Tabs

Dashboard with sections
 

File System Browser

File browser
 

Static Prefix Parameters

You can use static text prefixes before parameters in your route patterns. This is useful when you want to distinguish between different types of IDs or create more semantic URLs.

Basic Prefix Pattern

Static prefix parameters
 
Accessing prefixed parameters
 

Multiple Prefixed Parameters

You can combine multiple static prefix parameters in a single route:

Multiple prefixed parameters
 

Route Ordering with Prefixes

When using static prefix parameters, route order is critical. More specific routes with prefixes must be defined before generic parameter routes:

Route ordering with prefixes
 

Performance

Zero Runtime Overhead: Static prefix patterns are parsed and compiled into regular expressions when routes are defined (at initialization time), not on every navigation. This ensures optimal performance with no impact on route matching speed.

Use Cases

Common use cases
 

Best Practices

Route Order Matters

Routes are matched in the order they are defined. Place more specific routes before general ones:

Route ordering
 
Parameter Validation

Always validate parameters before using them. Users can type anything in the URL!

Parameter validation
 

Try It Live

Experiment with route parameters in our live examples: