From 74afe2a61a21d0bee5e5851d40ed24281a345ffe Mon Sep 17 00:00:00 2001 From: Jorge Alvarez Date: Wed, 31 Mar 2021 18:16:51 +0200 Subject: [PATCH] Fix bug when named params route starts with /. --- CHANGELOG.md | 4 ++++ package-lock.json | 4 ++-- package.json | 2 +- src/lib/utils.js | 13 +++---------- src/router/finder.js | 5 ++++- src/router/path.js | 9 ++++++++- test/spa_router.test.js | 3 ++- 7 files changed, 24 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4878f2..ad96184 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Svelte Router changelog +## 6.0.2 + +Fix bug with named routes starting with a /. + ## 6.0.1 Update type definitions. diff --git a/package-lock.json b/package-lock.json index 3a4f170..8eaf924 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "svelte-router-spa", - "version": "6.0.1", + "version": "6.0.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "svelte-router-spa", - "version": "6.0.1", + "version": "6.0.2", "license": "MIT", "dependencies": { "url-params-parser": "^1.0.3" diff --git a/package.json b/package.json index b3befa3..b194bb9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "svelte-router-spa", - "version": "6.0.1", + "version": "6.0.2", "description": "A full featured router component for Svelte and your Single Page Applications.", "main": "lib/cjs/index.js", "browser": "lib/index.js", diff --git a/src/lib/utils.js b/src/lib/utils.js index acbe117..525048f 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -159,20 +159,12 @@ const removeExtraPaths = (pathNames, basePathNames) => { **/ const removeSlash = (pathName, position = 'lead') => { - if (pathName.trim().length < 1) { - return ''; - } - if (position === 'trail' || position === 'both') { - if (pathName.slice(-1) === '/') { - pathName = pathName.slice(0, -1); - } + pathName = pathName.replace(/\/$/, ''); } if (position === 'lead' || position === 'both') { - if (pathName[0] === '/') { - pathName = pathName.slice(1); - } + pathName = pathName.replace(/^\//, ''); } return pathName; @@ -198,6 +190,7 @@ const routeNameLocalised = (route, language = null) => { **/ const startsWithNamedParam = (currentRoute) => { const routeName = removeSlash(currentRoute); + return routeName.startsWith(':'); }; diff --git a/src/router/finder.js b/src/router/finder.js index 6a792a8..672606f 100644 --- a/src/router/finder.js +++ b/src/router/finder.js @@ -2,7 +2,7 @@ import { UrlParser } from './url_parser'; import { RouterRedirect } from './redirect'; import { RouterRoute } from './route'; import { RouterPath } from './path'; -import { anyEmptyNestedRoutes, pathWithoutQueryParams, startsWithNamedParam } from '../lib/utils'; +import { anyEmptyNestedRoutes, pathWithoutQueryParams, removeSlash, startsWithNamedParam } from '../lib/utils'; const NotFoundPage = '/404.html'; @@ -45,6 +45,9 @@ function RouterFinder({ routes, currentUrl, routerOptions, convert }) { routes.forEach(function (route) { routerPath.updatedPath(route); + if (route.name !== '/') { + route.name = removeSlash(route.name); + } if (matchRoute(routerPath, route.name)) { let routePath = routerPath.routePath(); redirectTo = RouterRedirect(route, redirectTo).path(); diff --git a/src/router/path.js b/src/router/path.js index 4242efb..088b185 100644 --- a/src/router/path.js +++ b/src/router/path.js @@ -1,4 +1,11 @@ -import { updateRoutePath, getNamedParams, nameToPath, removeExtraPaths, routeNameLocalised } from '../lib/utils'; +import { + getNamedParams, + nameToPath, + updateRoutePath, + removeExtraPaths, + removeSlash, + routeNameLocalised +} from '../lib/utils'; function RouterPath({ basePath, basePathName, pathNames, convert, currentLanguage }) { let updatedPathRoute; diff --git a/test/spa_router.test.js b/test/spa_router.test.js index f897ae9..6c407f5 100644 --- a/test/spa_router.test.js +++ b/test/spa_router.test.js @@ -427,7 +427,7 @@ describe('Router', function () { nestedRoutes: [ { name: 'index', component: 'EmployeesIndex' }, { - name: 'show/:id/:full-name', + name: '/show/:id/:full-name', component: 'ShowEmployee', }, ], @@ -436,6 +436,7 @@ describe('Router', function () { }, ] }) + describe('When path is nested with named params', function () { let showEmployeeRoute let activeRoute