Skip to content

Commit

Permalink
fix: redirect in route should also join parent path
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Mar 6, 2020
1 parent 86e0ca5 commit 4667acb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
35 changes: 28 additions & 7 deletions packages/core/src/Route/Route.test.ts
Expand Up @@ -80,13 +80,19 @@ test('config routes with relative path', async () => {
expect(
await route.getRoutes({
config: {
routes: [{ path: '/foo', routes: [{ path: 'bar' }] }, { path: 'bar' }],
routes: [
{ path: '/foo', routes: [{ path: 'bar', redirect: 'hoo' }] },
{ path: 'bar', redirect: 'hoo' },
],
},
root: '/tmp',
}),
).toEqual([
{ path: '/foo', routes: [{ path: '/foo/bar', exact: true }] },
{ path: '/bar', exact: true },
{
path: '/foo',
routes: [{ path: '/foo/bar', redirect: '/foo/hoo', exact: true }],
},
{ path: '/bar', exact: true, redirect: '/hoo' },
]);
});

Expand All @@ -96,8 +102,17 @@ test('config routes with relative path (root path is also relative)', async () =
await route.getRoutes({
config: {
routes: [
{ path: 'foo', routes: [{ path: 'bar', routes: [{ path: 'bar' }] }] },
{ path: 'bar' },
{
path: 'foo',
routes: [
{
path: 'bar',
redirect: 'hoo',
routes: [{ path: 'bar', redirect: 'hoo' }],
},
],
},
{ path: 'bar', redirect: 'hoo' },
],
},
root: '/tmp',
Expand All @@ -106,10 +121,16 @@ test('config routes with relative path (root path is also relative)', async () =
{
path: '/foo',
routes: [
{ path: '/foo/bar', routes: [{ exact: true, path: '/foo/bar/bar' }] },
{
path: '/foo/bar',
redirect: '/foo/hoo',
routes: [
{ exact: true, path: '/foo/bar/bar', redirect: '/foo/bar/hoo' },
],
},
],
},
{ path: '/bar', exact: true },
{ path: '/bar', exact: true, redirect: '/hoo' },
]);
});

Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/Route/Route.ts
Expand Up @@ -79,6 +79,11 @@ class Route {
if (route.path && route.path.charAt(0) !== '/') {
route.path = winPath(join(opts.parentRoute?.path || '/', route.path));
}
if (route.redirect && route.redirect.charAt(0) !== '/') {
route.redirect = winPath(
join(opts.parentRoute?.path || '/', route.redirect),
);
}

if (route.routes) {
await this.patchRoutes(route.routes, {
Expand Down

0 comments on commit 4667acb

Please sign in to comment.