@@ -44,13 +44,13 @@ function getIndexRoute(route, location, callback) {
4444}
4545
4646function assignParams ( params , paramNames , paramValues ) {
47- return paramNames . reduceRight ( function ( params , paramName , index ) {
47+ return paramNames . reduce ( function ( params , paramName , index ) {
4848 const paramValue = paramValues && paramValues [ index ]
4949
5050 if ( Array . isArray ( params [ paramName ] ) ) {
51- params [ paramName ] . unshift ( paramValue )
51+ params [ paramName ] . push ( paramValue )
5252 } else if ( paramName in params ) {
53- params [ paramName ] = [ paramValue , params [ paramName ] ]
53+ params [ paramName ] = [ params [ paramName ] , paramValue ]
5454 } else {
5555 params [ paramName ] = paramValue
5656 }
@@ -63,34 +63,46 @@ function createParams(paramNames, paramValues) {
6363 return assignParams ( { } , paramNames , paramValues )
6464}
6565
66- function matchRouteDeep ( basename , route , location , callback ) {
66+ function matchRouteDeep (
67+ route , location , remainingPathname , paramNames , paramValues , callback
68+ ) {
6769 let pattern = route . path || ''
6870
69- if ( pattern . charAt ( 0 ) !== '/' )
70- pattern = basename . replace ( / \/ * $ / , '/' ) + pattern // Relative paths build on the parent's path.
71+ if ( pattern . charAt ( 0 ) === '/' ) {
72+ remainingPathname = location . pathname
73+ paramNames = [ ]
74+ paramValues = [ ]
75+ }
7176
72- const { remainingPathname, paramNames, paramValues } = matchPattern ( pattern , location . pathname )
73- const isExactMatch = remainingPathname === ''
77+ if ( remainingPathname !== null ) {
78+ const matched = matchPattern ( pattern , remainingPathname )
79+ remainingPathname = matched . remainingPathname
80+ paramNames = [ ...paramNames , ...matched . paramNames ]
81+ paramValues = [ ...paramValues , ...matched . paramValues ]
7482
75- if ( isExactMatch && route . path ) {
76- const match = {
77- routes : [ route ] ,
78- params : createParams ( paramNames , paramValues )
79- }
83+ if ( remainingPathname === '' && route . path ) {
84+ const match = {
85+ routes : [ route ] ,
86+ params : createParams ( paramNames , paramValues )
87+ }
8088
81- getIndexRoute ( route , location , function ( error , indexRoute ) {
82- if ( error ) {
83- callback ( error )
84- } else {
85- if ( Array . isArray ( indexRoute ) )
86- match . routes . push ( ...indexRoute )
87- else if ( indexRoute )
88- match . routes . push ( indexRoute )
89+ getIndexRoute ( route , location , function ( error , indexRoute ) {
90+ if ( error ) {
91+ callback ( error )
92+ } else {
93+ if ( Array . isArray ( indexRoute ) )
94+ match . routes . push ( ...indexRoute )
95+ else if ( indexRoute )
96+ match . routes . push ( indexRoute )
8997
90- callback ( null , match )
91- }
92- } )
93- } else if ( remainingPathname != null || route . childRoutes ) {
98+ callback ( null , match )
99+ }
100+ } )
101+ return
102+ }
103+ }
104+
105+ if ( remainingPathname != null || route . childRoutes ) {
94106 // Either a) this route matched at least some of the path or b)
95107 // we don't have to load this route's children asynchronously. In
96108 // either case continue checking for matches in the subtree.
@@ -109,7 +121,7 @@ function matchRouteDeep(basename, route, location, callback) {
109121 } else {
110122 callback ( )
111123 }
112- } , pattern )
124+ } , remainingPathname , paramNames , paramValues )
113125 } else {
114126 callback ( )
115127 }
@@ -130,15 +142,21 @@ function matchRouteDeep(basename, route, location, callback) {
130142 * Note: This operation may finish synchronously if no routes have an
131143 * asynchronous getChildRoutes method.
132144 */
133- function matchRoutes ( routes , location , callback , basename = '' ) {
145+ function matchRoutes (
146+ routes , location , callback ,
147+ remainingPathname = location . pathname , paramNames = [ ] , paramValues = [ ]
148+ ) {
134149 loopAsync ( routes . length , function ( index , next , done ) {
135- matchRouteDeep ( basename , routes [ index ] , location , function ( error , match ) {
136- if ( error || match ) {
137- done ( error , match )
138- } else {
139- next ( )
150+ matchRouteDeep (
151+ routes [ index ] , location , remainingPathname , paramNames , paramValues ,
152+ function ( error , match ) {
153+ if ( error || match ) {
154+ done ( error , match )
155+ } else {
156+ next ( )
157+ }
140158 }
141- } )
159+ )
142160 } , callback )
143161}
144162
0 commit comments