@@ -23,15 +23,22 @@ const Redirect = React.createClass({
23
23
if ( route . from )
24
24
route . path = route . from
25
25
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
-
32
26
route . onEnter = function ( nextState , replaceState ) {
33
27
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
+ }
35
42
36
43
replaceState (
37
44
route . state || location . state ,
@@ -41,6 +48,22 @@ const Redirect = React.createClass({
41
48
}
42
49
43
50
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
44
67
}
45
68
46
69
} ,
0 commit comments