@@ -21,47 +21,62 @@ const regexpCompileCache: {
2121export function createMatcher ( routes : Array < RouteConfig > ) : Matcher {
2222 const { pathMap, nameMap } = createRouteMap ( routes )
2323
24- function match ( raw : RawLocation , currentRoute ?: Route ) : Route {
24+ function match (
25+ raw : RawLocation ,
26+ currentRoute ?: Route ,
27+ redirectedFrom ?: Location
28+ ) : Route {
2529 const location = normalizeLocation ( raw , currentRoute )
2630 const { name } = location
2731
2832 if ( name ) {
2933 const record = nameMap [ name ]
3034 if ( record ) {
3135 location . path = fillParams ( record . path , location . params , `named route "${ name } "` )
32- return createRouteContext ( record , location )
36+ return createRouteContext ( record , location , redirectedFrom )
3337 }
3438 } else if ( location . path ) {
3539 location . params = { }
3640 for ( const path in pathMap ) {
3741 if ( matchRoute ( path , location . params , location . path ) ) {
38- return createRouteContext ( pathMap [ path ] , location )
42+ return createRouteContext ( pathMap [ path ] , location , redirectedFrom )
3943 }
4044 }
4145 }
4246 // no match
4347 return createRouteContext ( null , location )
4448 }
4549
46- function createRouteContext ( record : ?RouteRecord , location : Location ) : Route {
50+ function createRouteContext (
51+ record : ?RouteRecord ,
52+ location : Location ,
53+ redirectedFrom ?: Location
54+ ) : Route {
4755 if ( record && record . redirect ) {
48- return redirect ( record , location )
56+ return redirect ( record , redirectedFrom || location )
4957 }
5058 if ( record && record . matchAs ) {
5159 return alias ( record , location , record . matchAs )
5260 }
53- return Object . freeze ( {
61+ const route : Route = {
5462 name : location . name ,
5563 path : location . path || '/' ,
5664 hash : location . hash || '' ,
5765 query : location . query || { } ,
5866 params : location . params || { } ,
5967 fullPath : getFullPath ( location ) ,
6068 matched : record ? formatMatch ( record ) : [ ]
61- } )
69+ }
70+ if ( redirectedFrom ) {
71+ route . redirectedFrom = getFullPath ( redirectedFrom )
72+ }
73+ return Object . freeze ( route )
6274 }
6375
64- function redirect ( record : RouteRecord , location : Location ) : Route {
76+ function redirect (
77+ record : RouteRecord ,
78+ location : Location
79+ ) : Route {
6580 const { query, hash, params } = location
6681 const { redirect } = record
6782 const name = redirect && typeof redirect === 'object' && redirect . name
@@ -75,7 +90,7 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
7590 query,
7691 hash,
7792 params
78- } )
93+ } , undefined , location )
7994 } else if ( typeof redirect === 'string' ) {
8095 // 1. resolve relative redirect
8196 const rawPath = resolveRecordPath ( redirect , record )
@@ -87,14 +102,18 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
87102 path,
88103 query,
89104 hash
90- } )
105+ } , undefined , location )
91106 } else {
92107 warn ( `invalid redirect option: ${ JSON . stringify ( redirect ) } ` )
93108 return createRouteContext ( null , location )
94109 }
95110 }
96111
97- function alias ( record : RouteRecord , location : Location , matchAs : string ) : Route {
112+ function alias (
113+ record : RouteRecord ,
114+ location : Location ,
115+ matchAs : string
116+ ) : Route {
98117 const aliasedPath = fillParams ( matchAs , location . params , `aliased route with path "${ matchAs } "` )
99118 const aliasedMatch = match ( {
100119 _normalized : true ,
@@ -112,7 +131,11 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
112131 return match
113132}
114133
115- function matchRoute ( path : string , params : Object , pathname : string ) : boolean {
134+ function matchRoute (
135+ path : string ,
136+ params : Object ,
137+ pathname : string
138+ ) : boolean {
116139 let keys , regexp
117140 const hit = regexpCache [ path ]
118141 if ( hit ) {
@@ -140,16 +163,11 @@ function matchRoute (path: string, params: Object, pathname: string): boolean {
140163 return true
141164}
142165
143- function formatMatch ( record : ?RouteRecord ) : Array < RouteRecord > {
144- const res = [ ]
145- while ( record ) {
146- res . unshift ( record )
147- record = record . parent
148- }
149- return res
150- }
151-
152- function fillParams ( path : string , params : ?Object , routeMsg : string ) : string {
166+ function fillParams (
167+ path : string ,
168+ params : ?Object ,
169+ routeMsg : string
170+ ) : string {
153171 try {
154172 const filler =
155173 regexpCompileCache [ path ] ||
@@ -161,6 +179,15 @@ function fillParams (path: string, params: ?Object, routeMsg: string): string {
161179 }
162180}
163181
182+ function formatMatch ( record : ?RouteRecord ) : Array < RouteRecord > {
183+ const res = [ ]
184+ while ( record ) {
185+ res . unshift ( record )
186+ record = record . parent
187+ }
188+ return res
189+ }
190+
164191function resolveRecordPath ( path : string , record : RouteRecord ) : string {
165192 return resolvePath ( path , record . parent ? record . parent . path : '/' , true )
166193}
0 commit comments