Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support redirect in routing configuration #867

Merged
merged 2 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/en/routing/supplemental.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The routing configuration API is constructed with the following properties:
- `outlet: string`: The `outlet` name for the route. This is used by the `Outlet` widget to determine what needs to be rendered.
- `defaultRoute: boolean` (optional): Marks the outlet as default, the application will redirect to this route automatically if no route or an unknown route is found on application load.
- `defaultParams: { [index: string]: string }` (optional): Associated default parameters (`path` and `query`), required if the default route has required params.
- `redirect: string` (optional): A path to redirect to when matched exactly, params can be referenced from the matched route by the using the `{}` syntax
- `children: RouteConfig[]` (optional): Nested child routing configuration.

> src/routes.ts
Expand All @@ -25,6 +26,7 @@ export default [
id: 'about',
path: 'about',
outlet: 'about-overview',
redirect: 'about/company'
children: [
{
id: 'about-services',
Expand Down
12 changes: 11 additions & 1 deletion src/routing/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class Router extends Evented<{ nav: NavEvent; route: RouteEvent; outlet:
private _register(config: RouteConfig[], routes?: Route[], parentRoute?: Route): void {
routes = routes ? routes : this._routes;
for (let i = 0; i < config.length; i++) {
let { path, outlet, children, defaultRoute = false, defaultParams = {}, id, title } = config[i];
let { path, outlet, children, defaultRoute = false, defaultParams = {}, id, title, redirect } = config[i];
let [parsedPath, queryParamString] = path.split('?');
let queryParams: string[] = [];
parsedPath = this._stripLeadingSlash(parsedPath);
Expand All @@ -200,6 +200,7 @@ export class Router extends Evented<{ nav: NavEvent; route: RouteEvent; outlet:
title,
path: parsedPath,
segments,
redirect,
defaultParams: parentRoute ? { ...parentRoute.defaultParams, ...defaultParams } : defaultParams,
children: [],
fullPath: parentRoute ? `${parentRoute.fullPath}/${parsedPath}` : parsedPath,
Expand Down Expand Up @@ -348,6 +349,15 @@ export class Router extends Evented<{ nav: NavEvent; route: RouteEvent; outlet:
}

if (matchedRoute) {
if (matchedRoute.route.redirect && matchedRoute.type === 'index') {
let { redirect } = matchedRoute.route;
const params = { ...matchedRoute.params };
Object.keys(params).forEach((paramKey) => {
redirect = redirect.replace(`{${paramKey}}`, params[paramKey]);
});
this.setPath(redirect);
return;
}
if (matchedRoute.type === 'partial') {
matchedRoute.type = 'error';
}
Expand Down
2 changes: 2 additions & 0 deletions src/routing/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface Route {
params: string[];
segments: string[];
children: Route[];
redirect?: string;
fullPath: string;
fullParams: string[];
fullQueryParams: string[];
Expand All @@ -34,6 +35,7 @@ export interface RouteConfig {
path: string;
outlet: string;
children?: RouteConfig[];
redirect?: string;
defaultParams?: Params;
defaultRoute?: boolean;
title?: string;
Expand Down
30 changes: 30 additions & 0 deletions tests/routing/unit/Router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,4 +675,34 @@ describe('Router', () => {
assert.strictEqual(contextMap!.size, 2);
});
});

describe('redirect', () => {
it('should redirect to path on matched route', () => {
const router = new Router(
[
{
path: 'foo/{param}',
id: 'foo',
outlet: 'main',
redirect: 'foo/{param}/bar',
defaultParams: {
param: 'default-param'
},
defaultRoute: true,
children: [
{
path: 'bar',
id: 'bar',
outlet: 'bar'
}
]
}
],
{ HistoryManager }
);
const contextMap = router.getOutlet('bar');
assert.isOk(contextMap);
assert.strictEqual(contextMap!.size, 1);
});
});
});