Skip to content

Commit

Permalink
Passing a regex to match prefix for nested routers
Browse files Browse the repository at this point in the history
Fixes #134
  • Loading branch information
ItalyPaleAle committed Sep 19, 2020
1 parent b1f0ae0 commit b46197f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Advanced Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ Pre-conditions are defined in the routes object, using the `wrap` method exporte

The pre-condition functions receive a dictionary `detail` with the same structure as the `routeLoaded` event:

- `detail.route`: the route that was matched, exactly as defined in the route definition object
- `detail.component`: the Svelte component that is being evaluated (this is a JavaScript function)
- `detail.name`: name of the Svelte component (a string)
- `detail.location`: the current path (just like the `$location` readable store)
Expand Down Expand Up @@ -333,6 +334,8 @@ Both routes first load the `Hello` route, as they both match `/hello/*` in the o

Features like highlighting active links will still work, regardless of where those links are placed in the page (in which component).

Note that if your parent router uses a route that contains parameters, such as `/user/:id`, then you must define a regular expression for `prefix`. For example: `prefix={/^\/user\/[0-9]+/}`.

## Route groups

You can get route groups by creating a Svelte component which nests the other components. For example:
Expand Down
12 changes: 10 additions & 2 deletions Router.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,16 @@ class RouteItem {
*/
match(path) {
// If there's a prefix, remove it before we run the matching
if (prefix && path.startsWith(prefix)) {
path = path.substr(prefix.length) || '/'
if (prefix) {
if (typeof prefix == 'string' && path.startsWith(prefix)) {
path = path.substr(prefix.length) || '/'
}
else if (prefix instanceof RegExp) {
const match = path.match(prefix)
if (match && match[0]) {
path = path.substr(match[0].length) || '/'
}
}
}
// Check if the pattern matches
Expand Down

0 comments on commit b46197f

Please sign in to comment.