Skip to content

Commit

Permalink
fix(navigation-plan): Accept parameters in a redirect config
Browse files Browse the repository at this point in the history
All of the following will now work:

// Redirects 'first' to 'second/0'
{ route: 'first', redirect: 'second/0' }

// Redirects 'first/1' to 'second/1'
{ route: 'first/:foo', redirect: 'second/:foo' }

// Redirects 'first/1/2' to 'second'
{ route: 'first/:foo/:bar', redirect: 'second/:baz' }

{ route: 'second/:id?' }

closes #607
  • Loading branch information
davismj committed Jul 10, 2018
1 parent 1d12aff commit 7172ed2
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/navigation-plan.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,31 @@ export function _buildNavigationPlan(instruction: NavigationInstruction, forceLi
if ('redirect' in config) {
let router = instruction.router;
return router._createNavigationInstruction(config.redirect)
.then(newInstruction => {
let params = Object.keys(newInstruction.params).length ? instruction.params : {};
let redirectLocation = router.generate(newInstruction.config.name, params, instruction.options);
.then(newInstruction => {
let params = {};
for (let key in newInstruction.params) {

// If the param on the redirect points to another param, e.g. { route: first/:this, redirect: second/:this }
let val = newInstruction.params[key];
if (typeof(val) === 'string' && val[0] === ':') {
val = val.slice(1);

// And if that param is found on the original instruction then use it
if (val in instruction.params) {
params[key] = instruction.params[val];
}
} else {
params[key] = newInstruction.params[key];
}
}
let redirectLocation = router.generate(newInstruction.config.name, params, instruction.options);

if (instruction.queryString) {
redirectLocation += '?' + instruction.queryString;
}
if (instruction.queryString) {
redirectLocation += '?' + instruction.queryString;
}

return Promise.resolve(new Redirect(redirectLocation));
});
return Promise.resolve(new Redirect(redirectLocation));
});
}

let prev = instruction.previousInstruction;
Expand Down

0 comments on commit 7172ed2

Please sign in to comment.