Skip to content

Commit

Permalink
Fix bug when named params route starts with /.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgegorka committed Mar 31, 2021
1 parent 0ca4f07 commit 74afe2a
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Svelte Router changelog

## 6.0.2

Fix bug with named routes starting with a /.

## 6.0.1

Update type definitions.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
13 changes: 3 additions & 10 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -198,6 +190,7 @@ const routeNameLocalised = (route, language = null) => {
**/
const startsWithNamedParam = (currentRoute) => {
const routeName = removeSlash(currentRoute);

return routeName.startsWith(':');
};

Expand Down
5 changes: 4 additions & 1 deletion src/router/finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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();
Expand Down
9 changes: 8 additions & 1 deletion src/router/path.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 2 additions & 1 deletion test/spa_router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ describe('Router', function () {
nestedRoutes: [
{ name: 'index', component: 'EmployeesIndex' },
{
name: 'show/:id/:full-name',
name: '/show/:id/:full-name',
component: 'ShowEmployee',
},
],
Expand All @@ -436,6 +436,7 @@ describe('Router', function () {
},
]
})

describe('When path is nested with named params', function () {
let showEmployeeRoute
let activeRoute
Expand Down

0 comments on commit 74afe2a

Please sign in to comment.