Skip to content

Commit 1b0ab07

Browse files
committed
expose redirectedFrom on $route
1 parent d20c2eb commit 1b0ab07

File tree

4 files changed

+57
-29
lines changed

4 files changed

+57
-29
lines changed

flow/declarations.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ declare type Route = {
6666
params: StringHash;
6767
fullPath: string;
6868
matched: Array<RouteRecord>;
69+
redirectedFrom?: string;
6970
}
7071

7172
declare type Matcher = (location: RawLocation, current?: Route) => Route;

src/components/link.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default {
2323
const current = this.$route
2424
const to = normalizeLocation(this.to, current, this.append)
2525
const resolved = router.match(to)
26-
const fullPath = resolved.fullPath
26+
const fullPath = resolved.redirectedFrom || resolved.fullPath
2727
const base = router.history.base
2828
const href = base ? cleanPath(base + fullPath) : fullPath
2929
const classes = {}

src/create-matcher.js

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,62 @@ const regexpCompileCache: {
2121
export 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+
164191
function resolveRecordPath (path: string, record: RouteRecord): string {
165192
return resolvePath(path, record.parent ? record.parent.path : '/', true)
166193
}

test/e2e/specs/redirect.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ module.exports = {
55
.waitForElementVisible('#app', 1000)
66
.assert.count('li a', 6)
77
// assert correct href with base
8-
.assert.attributeContains('li:nth-child(1) a', 'href', '/redirect/foo')
9-
.assert.attributeContains('li:nth-child(2) a', 'href', '/redirect/foo?foo=bar')
10-
.assert.attributeContains('li:nth-child(3) a', 'href', '/redirect/bar')
11-
.assert.attributeContains('li:nth-child(4) a', 'href', '/redirect/baz')
12-
.assert.attributeContains('li:nth-child(5) a', 'href', '/redirect/with-params/123')
13-
.assert.attributeContains('li:nth-child(6) a', 'href', '/redirect/')
8+
.assert.attributeContains('li:nth-child(1) a', 'href', '/redirect/relative-redirect')
9+
.assert.attributeContains('li:nth-child(2) a', 'href', '/redirect/relative-redirect?foo=bar')
10+
.assert.attributeContains('li:nth-child(3) a', 'href', '/redirect/absolute-redirect')
11+
.assert.attributeContains('li:nth-child(4) a', 'href', '/redirect/named-redirect')
12+
.assert.attributeContains('li:nth-child(5) a', 'href', '/redirect/redirect-with-params/123')
13+
.assert.attributeContains('li:nth-child(6) a', 'href', '/not-found')
1414

1515
.assert.containsText('.view', 'default')
1616

0 commit comments

Comments
 (0)