@@ -23,15 +23,22 @@ const Redirect = React.createClass({
2323 if ( route . from )
2424 route . path = route . from
2525
26- // TODO: Handle relative pathnames, see #1658
27- invariant (
28- route . to . charAt ( 0 ) === '/' ,
29- '<Redirect to> must be an absolute path. This should be fixed in the future'
30- )
31-
3226 route . onEnter = function ( nextState , replaceState ) {
3327 const { location, params } = nextState
34- const pathname = route . to ? formatPattern ( route . to , params ) : location . pathname
28+
29+ let pathname
30+ if ( route . to . charAt ( 0 ) === '/' ) {
31+ pathname = formatPattern ( route . to , params )
32+ }
33+ else if ( ! route . to ) {
34+ pathname = location . pathname
35+ }
36+ else {
37+ let routeIndex = nextState . routes . indexOf ( route )
38+ let parentPattern = Redirect . getRoutePattern ( nextState . routes , routeIndex - 1 )
39+ let pattern = parentPattern . replace ( / \/ * $ / , '/' ) + route . to
40+ pathname = formatPattern ( pattern , params )
41+ }
3542
3643 replaceState (
3744 route . state || location . state ,
@@ -41,6 +48,22 @@ const Redirect = React.createClass({
4148 }
4249
4350 return route
51+ } ,
52+
53+ getRoutePattern ( routes , routeIndex ) {
54+ let parentPattern = ''
55+
56+ for ( let i = routeIndex ; i >= 0 ; i -- ) {
57+ let route = routes [ i ]
58+ let pattern = route . path || ''
59+ parentPattern = pattern . replace ( / \/ * $ / , '/' ) + parentPattern
60+
61+ if ( pattern . indexOf ( '/' ) === 0 ) {
62+ break
63+ }
64+ }
65+
66+ return '/' + parentPattern
4467 }
4568
4669 } ,
0 commit comments