Skip to content

Commit

Permalink
Fix route matching
Browse files Browse the repository at this point in the history
Take prefix into account and bail early if path does not start with prefix.
Fixes #169
  • Loading branch information
Simon Holthausen committed Dec 3, 2020
1 parent bacd869 commit b9fca47
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Router.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,25 @@ class RouteItem {
* @returns {null|Object.<string, string>} List of paramters from the URL if there's a match, or `null` otherwise.
*/
match(path) {
// If there's a prefix, remove it before we run the matching
// If there's a prefix, check if it matches the start of the path.
// If not, bail early, else remove it before we run the matching.
if (prefix) {
if (typeof prefix == 'string' && path.startsWith(prefix)) {
path = path.substr(prefix.length) || '/'
if (typeof prefix == 'string') {
if (path.startsWith(prefix)) {
path = path.substr(prefix.length) || '/'
}
else {
return null
}
}
else if (prefix instanceof RegExp) {
const match = path.match(prefix)
if (match && match[0]) {
path = path.substr(match[0].length) || '/'
}
else {
return null
}
}
}
Expand Down

0 comments on commit b9fca47

Please sign in to comment.