From b9fca47b537d4cba2ad739516e17737b8af8b1ce Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 3 Dec 2020 16:38:02 +0100 Subject: [PATCH] Fix route matching Take prefix into account and bail early if path does not start with prefix. Fixes #169 --- Router.svelte | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Router.svelte b/Router.svelte index 555b279..5fb8d4e 100644 --- a/Router.svelte +++ b/Router.svelte @@ -300,16 +300,25 @@ class RouteItem { * @returns {null|Object.} 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 + } } }